与熊猫系列

时间:2015-05-07 18:59:25

标签: python pandas time-series

我有一系列datetime个对象和一系列跨越数年的数据。 A可以创建Series对象并重新对其进行重新抽样,以便按月分组:

df=pd.Series(varv,index=dates)
multiMmean=df.resample("M", how='mean')
print multiMmean

然而,这是输出

2005-10-31    172.4
2005-11-30     69.3
2005-12-31    187.6
2006-01-31    126.4
2006-02-28    187.0
2006-03-31    108.3
...
2014-01-31     94.6
2014-02-28     82.3
2014-03-31    130.1
2014-04-30     59.2
2014-05-31     55.6
2014-06-30      1.2

这是该系列每个月的平均值列表。这不是我想要的。我想要12个值,一年中每个月都有一个,这些年来每个月都有一个平均值。我如何获得multiMmean

我尝试在resample("M",how='mean')上使用multiMmean并列出了理解但我无法让它发挥作用。我错过了什么?

谢谢。

1 个答案:

答案 0 :(得分:6)

以下为我工作:

# create some random data with datetime index spanning 17 months
s = pd.Series(index=pd.date_range(start=dt.datetime(2014,1,1), end = dt.datetime(2015,6,1)), data = np.random.randn(517))

In [25]:
# now calc the mean for each month
s.groupby(s.index.month).mean()
Out[25]:
1     0.021974
2    -0.192685
3     0.095229
4    -0.353050
5     0.239336
6    -0.079959
7     0.022612
8    -0.254383
9     0.212334
10    0.063525
11   -0.043072
12   -0.172243
dtype: float64

因此我们可以groupby datetimeindex的month属性并调用mean这将计算所有月份的平均值

相关问题