Isma
Isma

Reputation: 15200

Grouping and counting data by several columns in Pandas

I have the following dataframe:

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

It contains just two columns with some text categories as follows:

         Results_Disadvantaged    Results_Advantaged

0           Above Standard          Above Standard

1           Below Standard          Above Standard

2           Above Standard          Above Standard

[...]

I would like to pivot these columns to have something like this:

                                       Above Standard     Below Standard

0           Results_Disadvantaged          6530               334

1           Results_Advantaged             5532               555

[...]

I tried using a crosstab:

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

But the output is not quite what I'm looking for:

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

Upvotes: 0

Views: 43

Answers (1)

BENY
BENY

Reputation: 323306

IIUC

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

Upvotes: 2

Related Questions