'Elegant pandas way to stratified frequencies

I want to know if I can reach my desired result with a "better" way? This means with less steps (but readable code!) and some pandas in-build features.

That is the input data

>>> df
  name sex group
0    A   w     x
1    B   m     y
2    C   m     y
3    D   w     y
4    E   w     y
5    F   w     y
6    G   m     y
7    H   w     x

That is the desired output

      x    y
sex
 m    0    3
 w    2    3

My current solution

#!/usr/bin/env pyhton3
import pandas as pd

df = pd.DataFrame(
    {
        'name': list('ABCDEFGH'),  # 8 persons
        'sex': list('wmmwwwmw'),
        'group': list('xyyyyyyx')
    }
)

r = df[['group', 'sex']].value_counts().reset_index(name='n')
#   group sex  n
# 0     y   m  3
# 1     y   w  3
# 2     x   w  2

r.pivot(columns='group', index='sex')

The result is that:

         n
group    x    y
sex
m      NaN  3.0
w      2.0  3.0

At the end it is fine. But I thought that I can solve this with less steps and maybe with a tweak to pivot() or something else.



Solution 1:[1]

You want a crosstab:

pd.crosstab(df['sex'], df['group'])

output:

group  x  y
sex        
m      0  3
w      2  3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway