2016-06-28 172 views
1

總計數我有一個數據幀是這樣的:計算使用熊貓透視表

student  class  subject  date   status 

jack  class-1  maths  20150101  fail 
jack  class-1  maths  20150205  win 
jack  class-1  maths  20150310  fail 
jack  class-1  maths  20150415  fail 
mathew  class-2  maths  20150102  win 
mathew  class-2  maths  20150208  win 
mathew  class-2  maths  20150315  win 
john  class-3  maths  20150125  fail 

它是學生在不同日期的數學賽區的地位, 有些同學不要錯過特定日期的方案競賽。 我怎樣才能得到的結果是這樣使用熊貓pivot table功能

student  class  subject fail win 
jack  class-1  maths  3  1 
mathew  class-2  maths  0  3 
john  class-3  maths  1  0 

回答

0

您可以使用pivot_tablereset_index

df = df.pivot_table(index=['student','class','subject'], 
        columns='status', 
        values='date', 
        aggfunc=len, 
        fill_value=0).reset_index() 
print (df) 
status student class subject fail win 
0   jack class-1 maths  3 1 
1   john class-3 maths  1 0 
2  mathew class-2 maths  0 3 

最後你可以刪除(在pandas0.18.0新)由rename_axis名稱的列:

df = df.rename_axis(None, axis=1) 
#pandas bellow 0.18.0 
#df.columns.name = None 
print (df) 
    student class subject fail win 
0 jack class-1 maths  3 1 
1 john class-3 maths  1 0 
2 mathew class-2 maths  0 3