简体   繁体   中英

simple pivot table of pandas dataframe

I'm trying to do a seemingly very simple task. Given a dataframe:

daf = pd.DataFrame({'co':['g','r','b','r','g','r','b','g'], 'sh':['c','s','r','r','r','s','c','r']})

  co sh 0 gc 1 rs 2 br 3 rr 4 gr 5 rs 6 bc 7 gr 

I'd like to count the number of records with the unique combination of 'co' and 'sh' values and output as a table with rows ['g','r','b'] and columns ['c','s','r']

  csr g 1 0 2 r 0 1 1 b 1 0 1 

Can it be done using pivot_table?

Thank you,

It can be done more simply using pandas.crosstab :

>>> pandas.crosstab(d.co, d.sh)
sh  c  r  s
co         
b   1  1  0
g   1  2  0
r   0  1  2

You can do it with pivot_table , but it will give you NaN instead of 0 for missing combos. You need to specify len as the aggregating function:

>>> d.pivot_table(index='co', columns='sh', aggfunc=len)
sh   c  r   s
co           
b    1  1 NaN
g    1  2 NaN
r  NaN  1   2

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