Reputation: 15200
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
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