用 pandas 总结几个月
- 作者: Modest_
- 来源: 51数据库
- 2022-11-15
问题描述
我知道有一个简单的实现可以做到这一点,但我不记得语法了.有一个简单的 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
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
