python - 如何在 DataFrame 的 groupby 中增加行数

标签 python pandas

我需要计算 pandas DataFrame 中每个产品的 activity_months 数。到目前为止,这是我的数据和代码:

from pandas import DataFrame
from datetime import datetime
data = [
('product_a','08/31/2013')
,('product_b','08/31/2013')
,('product_c','08/31/2013')
,('product_a','09/30/2013')
,('product_b','09/30/2013')
,('product_c','09/30/2013')
,('product_a','10/31/2013')
,('product_b','10/31/2013')
,('product_c','10/31/2013')
]

product_df = DataFrame( data, columns=['prod_desc','activity_month'])

for index, row in product_df.iterrows():
  row['activity_month']= datetime.strptime(row['activity_month'],'%m/%d/%Y')
  product_df.loc[index, 'activity_month'] = datetime.strftime(row['activity_month'],'%Y-%m-%d')

product_df = product_df.sort(['prod_desc','activity_month'])

product_df['month_num'] = product_df.groupby(['prod_desc']).size()

但是,这会返回 month_num 的 NaN。

这是我想要得到的:

prod_desc    activity_month   month_num 
product_a       2014-08-31         1 
product_a       2014-09-30         2         
product_a       2014-10-31         3         
product_b       2014-08-31         1 
product_b       2014-09-30         2         
product_b       2014-10-31         3         
product_c       2014-08-31         1 
product_c       2014-09-30         2         
product_c       2014-10-31         3     

最佳答案

groupby 是正确的想法,但正确的方法是cumcount:

>>> product_df['month_num'] = product_df.groupby('product_desc').cumcount()
>>> product_df

  product_desc activity_month  prod_count    pct_ch  month_num
0    product_a     2014-01-01          53       NaN          0
3    product_a     2014-02-01          52 -0.018868          1
6    product_a     2014-03-01          50 -0.038462          2
1    product_b     2014-01-01          44       NaN          0
4    product_b     2014-02-01          43 -0.022727          1
7    product_b     2014-03-01          41 -0.046512          2
2    product_c     2014-01-01          36       NaN          0
5    product_c     2014-02-01          35 -0.027778          1
8    product_c     2014-03-01          34 -0.028571          2

如果您真的希望它以 1 开头,那么只需这样做:

>>> product_df['month_num'] = product_df.groupby('product_desc').cumcount() + 1

  product_desc activity_month  prod_count    pct_ch  month_num
0    product_a     2014-01-01          53       NaN          1
3    product_a     2014-02-01          52 -0.018868          2
6    product_a     2014-03-01          50 -0.038462          3
1    product_b     2014-01-01          44       NaN          1
4    product_b     2014-02-01          43 -0.022727          2
7    product_b     2014-03-01          41 -0.046512          3
2    product_c     2014-01-01          36       NaN          1
5    product_c     2014-02-01          35 -0.027778          2
8    product_c     2014-03-01          34 -0.028571          3

关于python - 如何在 DataFrame 的 groupby 中增加行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23792033/

相关文章:

python - 如何添加 keras dropout 层?

python - 如何自定义 pygments 的输出?

python - 如何使用 Panda 将 JSON 字符串中的所有元素输出到表中

python - pandas 系列类别数据类型与 pandas 类别数据类型之间的区别

python - 尝试将特定列乘以 Pandas DataFrame (Python) 中多行的一部分

python - 如何提取具有非零列值的行?

python - 如何在 views.py 中创建模型实例?

python - 在 Spyder 中定义无标题文档的结构

Python Pandas 从复杂字典中创建记录

python - 计算 2 个数据框中项目的出现次数,然后检查以比较 2 个日期