Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

python - Summing over months with 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))

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...