Summing over months with pandas(用 pandas 总结几个月)
本文介绍了用 pandas 总结几个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道有一个简单的实现可以做到这一点,但我不记得语法了.有一个简单的 pandas 时间序列,我想按月汇总数据.具体来说,我想添加数月和数年的数据以获取一些摘要.可以用切片来写,但我记得看到过自动执行它的语法.
I know there is a simple implementation to do this but I cannot remember the syntax. Have a simple pandas time series and I want to summarize the data by month. Specifically I want to add data over months and years to get some summary of it. Can write it with slicing, but I remember seeing syntax that does it automatically.
import pandas as pd
df = Series(randn(100), index=pd.date_range('2012-01-01', periods=100))
一等奖是一等奖.
部分答案:
ds.resample('M', how=sum) # for calendar monthly
ds.resample('A', how=sum) # for calendar yearly
知道如何优雅地按年总和进行多索引吗?
Any idea how to elegantly get to multindexed by year sums?
推荐答案
In [1]: import pandas as pd
from numpy.random import randn
In [2]: df = Series(randn(500), index=pd.date_range('2012-01-01', periods=500))
In [3]: s2 = df.groupby([lambda x: x.year, lambda x: x.month]).sum()
In [4]: s2
Out[4]:
2012 1 3.853775
2 4.259941
3 4.629546
4 -10.812505
5 -16.383818
6 -5.255475
7 5.901344
8 13.375258
9 1.758670
10 6.570200
11 6.299812
12 7.237049
2013 1 -1.331835
2 3.399223
3 2.011031
4 7.905396
5 1.127362
dtype: float64
这篇关于用 pandas 总结几个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:用 pandas 总结几个月


基础教程推荐
猜你喜欢
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01