2016-06-28 84 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