python - 使用 pandas 计算时间序列中每个月的平均值

标签 python pandas

我有一个简单的时间序列:

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/

相关文章:

python - 如何使用 pandas 创建新的 df 列以使用正则表达式获取列名称和值?

python - 在python中按用户计算并发 session

python - 如何仅针对 dynamodb 流中的 INSERT 事件触发 lambda 函数?

python - 如何在 Python 中写入/打包二进制字符串到文件

python - 在 Anaconda 命令提示符中运行 Python 脚本的 Windows 快捷方式

python - python pandas 需要帮助来减少代码行数和周期时间

两个不同目录中的 Python(一个指定目录和一个隐藏目录)

python - 使用 Python-Treetaggerwrapper 进行分块

python - 类型错误 : setup() got an unexpected keyword argument 'stage'

python - 使用 AM/PM 和 +UCT 转换时间 (Python)