2016-04-15 161 views
0

我有一個表如以下蟒的表:(目前該表被過濾以僅顯示1個訪問者)樞軸採用熊貓

vstid vstrseq date  page timespent 

1  1  1/1/16  a  20.00 
1  1  1/1/16  b  3.00 
1  1  1/1/16  c  131.00 
1  1  1/1/16  d  .000 
1  1  1/1/16  a  3.00 

我想這樣:

vstid vstrseq  date  a b c d 
1  1   1/1/16 23 3 131 0 

我試圖創建一個數據幀像下面並試圖轉動它:

ptable=pd.DataFrame(table,columns= ['vstid','vstrseq','date','page','timespent']) 
pvtable=pd.pivot_table(ptable,index='vstid','vstrseq','date'],columns='page',values='timespent',aggfunc=np.sum) 

我有錯誤味精象下面這樣:

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)() 

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)() 

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12280)() 

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12231)() 

KeyError: 'TIMESPENT'` 

回答

0

您剛剛支架在你的索引定義丟失:

import pandas as pd 
import numpy as np 

df2 = pd.DataFrame({ 'vstid' : 1., 
        'vstrseq' : 1, 
        'date' : "1/1/16", 
        'page' : pd.Categorical(["a","b","c","d", "a"]), 
        'timespent' : pd.Categorical([20.00,3.0, 131.0,0.0, 3.0])}) 
print df2 

df3=pd.pivot_table(df2,index=['vstid','vstrseq','date'],columns='page',values='timespent',aggfunc=np.sum) 

print df3 
+0

代替手工輸入,我們可以自動將數據幀挑取決於表中的值 – totalzoom