Rotary columns while maintaining original column headers

I would like to count the number of yes and no values ​​by column and groupby index.

I have this framework

col0  col1 col2
A     yes  no
A     no   no
B     yes  yes
B     yes  no

I want it:

   col1     col2
   yes  no  yes  no
A  1    1   0    2
B  2    0   1    1

I tried with df.pivot_table(index='my_index', aggfunc='count') but I just got

   col1     col2

A  2        2
B  2        2
+4
source share
1 answer

Option 1
pd.get_dummies + groupby+sum

v = pd.get_dummies(df.set_index('col0'))

v.columns = pd.MultiIndex.from_tuples(
    list(map(tuple, v.columns.str.split('_')))
)
v.sum(level=0)

     col1     col2    
       no yes   no yes
col0                  
A       1   1    2   0
B       0   2    1   1

Option 2
stack + get_dummies+unstack

(df.set_index('col0')
   .stack()
   .str.get_dummies()
   .sum(level=[0,1])
   .unstack(-1)
   .swaplevel(0, 1, axis=1)
   .sort_index(level=0, axis=1)
)

     col1     col2    
       no yes   no yes
col0                  
A       1   1    2   0
B       0   2    1   1

Option 3
crosstab + concatby @Wen

i = pd.crosstab(df.col0, df.col1.astype('category'))
j = pd.crosstab(df.col0, df.col2.astype('category'))

pd.concat([i, j], axis=1, keys=['col1','col2'])

     col1     col2    
col1   no yes   no yes
col0                  
A       1   1    2   0
B       0   2    1   1
+3
source

Source: https://habr.com/ru/post/1695586/


All Articles