本文介绍了 pandas 系列的每月平均数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datetime对象序列和一系列跨越几年的数据. A可以创建一个Series对象并对其重新采样以按月分组:

I have a sequence of datetime objects and a series of data which spans through several years. A can create a Series object and resample it to group it by months:

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的内容?

which is a list of the mean value for each month of the series. This is not what I want. I want 12 values, one for every month of the year with a mean for each month through the years. How do I get that for multiMmean?

我尝试在multiMmean上使用resample("M",how='mean')并列出理解,但是我无法使其正常工作.我想念什么?

I have tried using resample("M",how='mean') on multiMmean and list comprehensions but I cannot get it to work. What am I missing?

谢谢.

推荐答案

以下内容对我有用:

# 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这将计算所有月份的平均值

So we can groupby the month attribute of the datetimeindex and call mean this will calculate the mean for all months

这篇关于 pandas 系列的每月平均数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:19