简体   繁体   中英

Pivot columns while retaining original column headers

I would like to count number of yes and no values by Column and groupby index.

I have this dataframe :

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

I want this:

   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 only got

   col1     col2

A  2        2
B  2        2

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 + concat by @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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM