python pandasivot_table在一列中计数频率 [英] python pandas pivot_table count frequency in one column

查看:53
本文介绍了python pandasivot_table在一列中计数频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python pandas的ivot_table还是陌生的,并且想问一种方法来计算一列中的值频率,该列也链接到另一列ID. DataFrame如下所示.

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 DataFrame looks like the following.

import pandas as pd
df = pd.DataFrame({'Account_number':[1,1,2,2,2,3,3],
                   'Product':['A', 'A', 'A', 'B', 'B','A', 'B']
                  })

对于输出,我想得到如下内容:

For the output, I'd like to get something like the following:

                Product
                A      B
Account_number           
      1         2      0
      2         1      2
      3         1      1

到目前为止,我尝试了以下代码:

So far, I tried this code:

df.pivot_table(rows = 'Account_number', cols= 'Product', aggfunc='count')

这段代码给了我两个相同的东西.上面的代码有什么问题?我问这个问题的部分原因是,这个DataFrame只是一个例子.我正在处理的实际数据有成千上万的account_numbers.感谢您的提前帮助!

This code gives me the two same things. What is the problems with the code above? A part of the reason why I am asking this question is that this DataFrame is just an example. The real data that I am working on has tens of thousands of account_numbers. Thanks for your help in advance!

推荐答案

您需要将aggfunc指定为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

看起来像是计数,正在计数每一列(Account_numberProduct)的实例,我不清楚这是否是一个错误...

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...

这篇关于python pandasivot_table在一列中计数频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆