diedro
diedro

Reputation: 613

pandas put in a dataframe the daily hour (and only the hour) where there is the max

I have this dataframe:

dates,rr.price,ax.price,be.price
2018-01-01 00:00:00,45.73,45.83,47.63
2018-01-01 01:00:00,44.16,44.59,44.42
2018-01-01 02:00:00,42.24,40.22,42.34
2018-01-01 03:00:00,39.29,37.31,38.36
2018-01-01 04:00:00,36.0,32.88,36.87
2018-01-01 05:00:00,41.99,39.27,39.79
2018-01-01 06:00:00,42.25,43.62,42.08
2018-01-01 07:00:00,44.97,49.69,51.19
2018-01-01 08:00:00,45.0,49.98,59.69
2018-01-01 09:00:00,44.94,48.04,56.67
2018-01-01 10:00:00,45.04,46.85,53.54
2018-01-01 11:00:00,46.67,47.95,52.6
2018-01-01 12:00:00,46.99,46.6,50.77
2018-01-01 13:00:00,44.16,43.02,50.27
2018-01-01 14:00:00,45.26,44.2,50.64
2018-01-01 15:00:00,47.84,47.1,54.79
2018-01-01 16:00:00,50.1,50.83,60.17
2018-01-01 17:00:00,54.3,58.31,59.47
2018-01-01 18:00:00,51.91,63.5,60.16
2018-01-01 19:00:00,51.38,61.9,70.81
2018-01-01 20:00:00,49.2,59.62,62.65
2018-01-01 21:00:00,45.73,52.84,59.71
2018-01-01 22:00:00,44.84,51.43,50.96
2018-01-01 23:00:00,38.11,45.35,46.52
2018-01-02 00:00:00,19.19,41.61,49.62
2018-01-02 01:00:00,14.99,40.78,45.05
2018-01-02 02:00:00,11.0,39.59,45.18
2018-01-02 03:00:00,10.0,36.95,37.12
2018-01-02 04:00:00,11.83,31.38,38.03
2018-01-02 05:00:00,14.99,34.02,46.17
2018-01-02 06:00:00,40.6,41.27,51.71
2018-01-02 07:00:00,46.99,48.25,54.37
2018-01-02 08:00:00,47.95,43.57,75.3
2018-01-02 09:00:00,49.9,48.34,68.48
2018-01-02 10:00:00,50.0,48.01,61.94
2018-01-02 11:00:00,49.7,52.22,63.26
2018-01-02 12:00:00,48.16,47.47,59.41
2018-01-02 13:00:00,47.24,47.61,60.0
2018-01-02 14:00:00,46.1,49.12,67.44
2018-01-02 15:00:00,47.6,52.38,66.82
2018-01-02 16:00:00,50.45,58.35,72.17
2018-01-02 17:00:00,54.9,61.4,70.28
2018-01-02 18:00:00,57.18,54.58,62.63
2018-01-02 19:00:00,54.9,53.66,63.78
2018-01-02 20:00:00,51.2,54.15,63.08
2018-01-02 21:00:00,48.82,48.67,56.42
2018-01-02 22:00:00,45.14,47.46,49.85
2018-01-02 23:00:00,40.09,42.46,43.87
2018-01-03 00:00:00,42.75,34.72,25.51
2018-01-03 01:00:00,35.02,30.31,21.07
2018-01-03 02:00:00,28.85,25.35,16.8

I want to have another dataframe where there is for each day the hour of the day where there is the max value of rr.price,dates,rr.price,be.price.

What I have done so far is this:

im = 1
dfr_im  = dfr[dfr.index.month == im]

because I want to do that for each month of my original dataframe where I have an entire year.

After that, I do:

dfr_h = dfr_im.groupby(dfr_im.index.date)['rr.price','ax.price','be.price'].idxmax()

This is result:

,rr.price,ax.price,be.price
2018-01-01,2018-01-01 17:00:00,2018-01-01 18:00:00,2018-01-01 19:00:00
2018-01-02,2018-01-02 18:00:00,2018-01-02 17:00:00,2018-01-02 08:00:00
2018-01-03,2018-01-03 00:00:00,2018-01-03 00:00:00,2018-01-03 00:00:00

However, I would like to have

,rr.price,ax.price,be.price
2018-01-01,17,18,19
2018-01-02,18,17,8
2018-01-03,0,0,0

In addition I would like to not only considering all 24 hours in a day but as additional columns I would like to consider only some hour in order to compute the hour of the day with the max value. For example I would like to consider the hours between [0-8] or [0-8+20-23].

Thanks

Upvotes: 0

Views: 53

Answers (1)

It_is_Chris
It_is_Chris

Reputation: 14063

you can stack get the hour and unstack

dfr_im.groupby(dfr_im.index.date)[['rr.price','ax.price','be.price']].idxmax().stack().dt.hour.unstack()

You can use between_time and then do the computation above on that slice. If you only want to look at one time frame then:

df_f = dfr_im.between_time('00:00', '08:00')

df_f.groupby(df_f.index.date)[['rr.price','ax.price','be.price']].idxmax().stack().dt.hour.unstack()

Or if you want to look at two times you can use loc with numpy's concatenate function

df_f = dfr_im.loc[np.concatenate([dfr_im.between_time('00:00', '08:00').index,
                                  dfr_im.between_time('20:00', '23:00').index])]

df_f.groupby(df_f.index.date)[['rr.price','ax.price','be.price']].idxmax().stack().dt.hour.unstack()

Upvotes: 1

Related Questions