我有一个简单的时间序列:
Month
1980-01-01 28.687977
1980-02-01 27.928480
1980-03-01 32.526912
1980-04-01 37.376463
1980-05-01 37.920970
1980-06-01 42.673177
1980-07-01 44.158804
1980-08-01 52.697249
1980-09-01 47.518417
1980-10-01 38.845849
1980-11-01 31.685959
1980-12-01 26.267851
1981-01-01 28.861739
1981-02-01 28.600699
1981-03-01 32.664966
1981-04-01 39.000000
1981-05-01 39.484174
1981-06-01 42.755117
1981-07-01 46.658333
1981-08-01 56.062465
...
其中 1980-02-01 是 1980 年 2 月 1 日。
事情一直在继续。
我想获得每个月的平均值。
Month
是一个 DatetimeIndex
因此我可以例如执行 df['1980'].mean()
来获取 a 的平均值给定年份。我需要的是每个月的平均值,但我不知道如何指定月份的选择器 df['???'].mean()
最佳答案
使用DatetimeIndex.month
和聚合平均值
:
s.groupby(s.index.month).mean()
关于python - 使用 pandas 计算时间序列中每个月的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53460376/