python - 您如何遍历 pandas Dataframe 中的组,对每个组进行操作,然后将值分配给原始 Dataframe?

标签 python pandas pandas-groupby

    yearCount = df[['antibiotic', 'order_date', 'antiYearCount']]

    yearGroups = yearCount.groupby('order_date')

    for year in yearGroups:
        yearCount['antiYearCount'] =year.groupby('antibiotic'['antibiotic'].transform(pd.Series.value_counts)

在这种情况下,yearCount 是一个包含 'order_date'、'antibiotic'、'antiYearCount' 的数据框。我已将 'order_date' 清理为仅包含订单年份。我想按 'order_date' 中的年份对 yearCount 进行分组,计算每个 'antibiotic' 在每个“年组”中出现的次数然后将该值分配给 yearCount'antiYearCount' 变量。

最佳答案

我认为您需要将新列 order_date 添加到 groupby 然后也可以使用 size 代替 pd.Series.value_counts 对于相同的输出:

df = pd.DataFrame({'antibiotic':list('accbbb'),
                   'antiYearCount':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'order_date': pd.to_datetime(['2012-01-01']*3+['2012-01-02']*3)})

print (df)
   C  D  E  antiYearCount antibiotic order_date
0  7  1  5              4          a 2012-01-01
1  8  3  3              5          c 2012-01-01
2  9  5  6              4          c 2012-01-01
3  4  7  9              5          b 2012-01-02
4  2  1  2              5          b 2012-01-02
5  3  0  4              4          b 2012-01-02

#copy for remove warning
#https://stackoverflow.com/a/45035966/2901002
yearCount = df[['antibiotic', 'order_date', 'antiYearCount']].copy()
yearCount['antiYearCount'] = yearCount.groupby(['order_date','antibiotic'])['antibiotic'] \
                                      .transform('size')
print (yearCount)
  antibiotic order_date  antiYearCount
0          a 2012-01-01              1
1          c 2012-01-01              2
2          c 2012-01-01              2
3          b 2012-01-02              3
4          b 2012-01-02              3
5          b 2012-01-02              3

yearCount['antiYearCount'] = yearCount.groupby(['order_date','antibiotic'])['antibiotic'] \
                                      .transform(pd.Series.value_counts)
print (yearCount)
  antibiotic order_date  antiYearCount
0          a 2012-01-01              1
1          c 2012-01-01              2
2          c 2012-01-01              2
3          b 2012-01-02              3
4          b 2012-01-02              3
5          b 2012-01-02              3

关于python - 您如何遍历 pandas Dataframe 中的组,对每个组进行操作,然后将值分配给原始 Dataframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45364063/

相关文章:

python - 使用 CNAME 时 Web 请求出现 SSL 错误

python - 在 scrapy 框架 python 中解析 JSON 响应的最佳方法

python - 函数为通过的 pandaDF 列制作具有正态曲线的直方图

python - 在这种情况下如何使用 get_dummies() ?

python - 如何在Python中进行时间序列分析时组合两个折线图进行数据验证

python - 如何指定对其他列进行操作的 pandas groupby 和聚合操作?

python - 如何使用 Wikipedia API 获取图像标题

pandas-groupby - xarray数据集分组的分位数方法

python - 计算数据框 Pandas 的多列中某个值的出现次数

python - 带有 Jupyter Notebook : Shortcut for “run all” ? 的 Visual Studio 代码