在Pandas中按几列对数据进行分组和计数

时间:2018-03-05 21:53:38

标签: python pandas

我有以下数据框:

ks2_question1_cat_df = ks2_question1_df[['Results_Disadvantaged', 'Results_Advantaged']]

它只包含两列,其中包含一些文本类别,如下所示:

         Results_Disadvantaged    Results_Advantaged

0           Above Standard          Above Standard

1           Below Standard          Above Standard

2           Above Standard          Above Standard

[...]

我想将这些列调整为具有以下内容:

                                       Above Standard     Below Standard

0           Results_Disadvantaged          6530               334

1           Results_Advantaged             5532               555

[...]

我尝试使用交叉表:

pd.crosstab(ks2_question1_df['Results_Disadvantaged'],ks2_question1_df['Results_Advantaged'])

但输出并不是我想要的:

Results_Advantaged      Above Standard  Below Standard
Results_Disadvantaged       
Above Standard               6104           84
Below Standard               2803           489

1 个答案:

答案 0 :(得分:2)

IIUC

df.apply(pd.value_counts).T
Out[1224]: 
                       AboveStandard  BelowStandard
Results_Disadvantaged            2.0            1.0
Results_Advantaged               3.0            NaN