python - Pandas DF 按月和日计算的所有年份平均值
问题描述
我在名为“aodata”(25627,4)的 df 中有这样的数据
Year Month Day Index
0 1950 1 1 -2.511
1 1950 1 2 -1.505
2 1950 1 3 -1.173
3 1950 1 4 -1.251
4 1950 1 5 -0.126
... ... .. ...
25622 2020 2 25 1.836
25623 2020 2 26 1.724
25624 2020 2 27 2.151
25625 2020 2 28 1.848
25626 2020 2 29 1.741
我需要找到“索引”列的长期每日平均值(来自所有年份),以便我的输出在特定月份(如 2 月)(月份 ==2)看起来像这样:
Month Day AvgIndex
2 1 -5.43
2 2 -5.29
2 3 -4.15
... ... ...
2 29 3.46
我尝试了不同的组合
cmao_mean = aodata[(aodata.Month == month)&(aodata.Day)].Index.mean() #gives a single number of the average of all the days in the month.
而且,我试图用这样的 for 循环来做到这一点:
days = list(range(1,29))
for x in days:
dmao = aodata[(aodata.Month == month)&(aodata.Day)].Index.mean(x)
这给了我这样的错误:
ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'>
我是 Python 新手,但我正在学习。感谢您对此的任何帮助!
解决方案
推荐阅读
- python-3.x - 如何从通过输入功能输入的列表中删除非唯一元素?
- php - PHP 中带有关联数组的 UPDATE 语句返回 注意
- arrays - 如何将 UIImage 数组保存到 Firebase 存储(以及用于字符串表示的数据库)中?
- r - system() 看不到 PATH 变量
- powerbi-embedded - 如何从本地 SSAS 读取/访问 Power Bi Embedded Report 查询字符串参数?
- apache-spark - 无法创建火花上下文 - SparkContext 挂起,Windows 操作系统
- php - 如何根据域使用动态 URL / 路由?
- azure-devops - 无法在托管代理上的 azure 管道任务中连接到 localdb
- javascript - 递归 setTimeout 调用是否会导致闭包链内存泄漏?
- c# - 当我尝试从中删除一行时,为什么我的文本文件不保存它的更改?