python - 如何正确使用 groupby 和 grouper 逐月累加列 'A' 和平均列 'B'

标签 python pandas pandas-groupby

我有一个包含 3 列的 pandas 数据:

日期:从 2018 年 1 月 1 日到 2019 年 8 月 23 日,A 列和 B 列。

import pandas as pd
df = pd.DataFrame(np.random.randint(0,10,size=(600, 2)), columns=list('AB'))
df['date'] = pd.DataFrame(pd.date_range(start='1/1/2018', end='8/23/2019'))
df.set_index('date')

df如下:

date        A   B
2018-01-01  7   4
2018-01-02  5   4
2018-01-03  3   1
2018-01-04  9   3
2018-01-05  7   8
2018-01-06  0   0
2018-01-07  6   8
2018-01-08  3   7
...
...
...
2019-08-18  1   0
2019-08-19  8   1
2019-08-20  5   9
2019-08-21  0   7
2019-08-22  3   6
2019-08-23  8   6

我想要A列的每月累计值和B列的每月平均值。最终输出将成为一个具有 20 行(2018 年的 12 个月和 2019 年的 8 个月)和 4 列的 df,代表每月累计 A 列的值、B 列的月平均值 值、月份数年份数 就像下面这样:

  month year  monthly_accumulated_of_A  monthly_averaged_of_B
0   1   2018    176                     1.747947
1   2   2018    110                     2.399476
2   3   2018    131                     3.976747
3   4   2018    227                     2.314923
4   5   2018    234                     0.464097
5   6   2018    249                     1.662753
6   7   2018    121                     1.588865
7   8   2018    165                     2.318268
8   9   2018    219                     1.060595
9   10  2018    131                     0.577268
10  11  2018    179                     3.948414
11  12  2018    115                     1.750346
12  1   2019    190                     3.364003
13  2   2019    215                     0.864792
14  3   2019    231                     3.219739
15  4   2019    186                     2.904413
16  5   2019    232                     0.324695
17  6   2019    163                     1.334139
18  7   2019    238                     1.670644
19  8   2019    112                     1.316442

​ 我怎样才能在 pandas 中实现这一点?

最佳答案

使用DataFrameGroupBy.aggDatetimeIndex.monthDatetimeIndex.year ,订购请添加 sort_index最后一次使用 reset_index对于来自 MultiIndex 的列:

import pandas as pd
import numpy as np

np.random.seed(2018)
#changed 300 to 600
df = pd.DataFrame(np.random.randint(0,10,size=(600, 2)), columns=list('AB'))
df['date'] = pd.DataFrame(pd.date_range(start='1/1/2018', end='8/23/2019'))
df = df.set_index('date')


df1 = (df.groupby([df.index.month.rename('month'), 
                 df.index.year.rename('year')])
       .agg({'A':'sum', 'B':'mean'})
       .sort_index(level=['year', 'month'])
       .reset_index())
<小时/>
print (df1)
    month  year    A         B
0       1  2018  147  4.838710
1       2  2018  120  3.678571
2       3  2018  114  4.387097
3       4  2018  143  3.800000
4       5  2018  124  3.870968
5       6  2018  129  4.700000
6       7  2018  143  3.935484
7       8  2018  118  5.483871
8       9  2018  150  5.500000
9      10  2018  139  4.225806
10     11  2018  136  4.933333
11     12  2018  141  4.548387
12      1  2019  137  4.709677
13      2  2019  120  4.964286
14      3  2019  167  4.935484
15      4  2019  121  4.200000
16      5  2019  133  4.129032
17      6  2019  140  5.066667
18      7  2019  189  4.677419
19      8  2019  100  3.695652

关于python - 如何正确使用 groupby 和 grouper 逐月累加列 'A' 和平均列 'B',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52258002/

相关文章:

python计数数据框列值满足条件

python - 如何根据 Pandas DataFrame 中的条件添加每组具有重复值的新列?

python - Telnet 自动化/脚本

python - Pandas 数据框中的文本拆分列表

pandas - 在多索引级别对pandas .diff()进行分区

python - 加速 pd.concat 的方法或使用其他方法连接表

python - Pandas DataFrame.Groupby.Agg 字典中的自定义列选择

python - 来自 df 的 Pandas 样本保持群体平衡

python - 使用 Python 列表推导计算列表中的正整数元素

python - 如何在 python 中的线程之间安全地传递变量值