pandas 与熊猫总结数月

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17450313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:58:04  来源:igfitidea点击:

Summing over months with pandas

pythonpandastime-series

提问by Joop

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.

我知道有一个简单的实现来做到这一点,但我不记得语法。有一个简单的Pandas时间序列,我想按月总结数据。具体来说,我想添加数月和数年的数据以获取一些摘要。可以用切片来编写它,但我记得看到自动执行它的语法。

import pandas as pd
df = Series(randn(100), index=pd.date_range('2012-01-01', periods=100))

a Multi-indexed Series with Years and sub endexed to months would be first prize.

带有年份和分到月份的多索引系列将是一等奖。

Partial Answer:

部分答案:

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?

知道如何优雅地按年总和进行多重索引吗?

回答by Jeff

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