将奇数天和偶数天分组 [英] Grouping odd and even days
本文介绍了将奇数天和偶数天分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的熊猫数据框
I have a pandas dataframe as the following
data
Out[8]:
value1
Date
2015-03-31 09:53:53.800 NaN
2015-03-31 10:28:54.700 1.34
2015-03-31 10:34:35.720 NaN
2015-03-31 10:36:53.540 1.26
2015-04-01 11:37:11.620 1.44
2015-04-01 11:39:30.520 NaN
2015-04-01 11:50:25.620 1.76
2015-04-02 11:50:30.620 1.38
2015-04-02 12:31:20.220 1.76
2015-04-02 12:37:43.940 2.36
2015-04-03 12:38:45.820 1.46
2015-04-03 12:41:56.680 2.26
2015-04-04 13:04:50.740 1.16
2015-04-05 12:38:45.820 1.46
2015-04-05 12:41:56.680 2.26
2015-04-05 13:04:50.740 1.16
我想计算属于第 0-2-4 天的值的平均值和属于第 1-3-5 天的值的平均值我如何在日期上使用 groupby 来这样做?
and I would like to compute the mean of the values belonging to the days 0-2-4 and the mean of the values belonging to the days 1-3-5 How can I use groupby on the dates to do so?
df2 = data.groupby(?).agg(np.mean)
推荐答案
试试这个:
In [7]: df2 = data.groupby(data.index.day % 2).agg(np.mean)
In [8]: df2
Out[8]:
value1
Date
0 1.665
1 1.600
说明:
In [9]: data.index.day
Out[9]: Int64Index([31, 31, 31, 31, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 5], dtype='int64', name='Date')
In [10]: data.index.day % 2
Out[10]: Int64Index([1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1], dtype='int64', name='Date')
这篇关于将奇数天和偶数天分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文