python pandas pivot_table count frequency in one column

前端 未结 4 1014
暗喜
暗喜 2020-12-08 00:59

I am still new to Python pandas\' pivot_table and would like to ask a way to count frequencies of values in one column, which is also linked to another column of ID. The Dat

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 01:33

    You need to specify the aggfunc as len:

    In [11]: df.pivot_table(index='Account_number', columns='Product', 
                            aggfunc=len, fill_value=0)
    Out[11]:
    Product         A  B
    Account_number
    1               2  0
    2               1  2
    3               1  1
    

    It looks like count, is counting the instances of each column (Account_number and Product), it's not clear to me whether this is a bug...

提交回复
热议问题