Grouping odd and even days

2019-07-09 06:53发布

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     

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)

1条回答
Evening l夕情丶
2楼-- · 2019-07-09 07:29

Try this:

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

Explanation:

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')
查看更多
登录 后发表回答