Skip to content Skip to sidebar Skip to footer

Groupby Every 2 Hours Data Of A Dataframe

I have a dataframe: Time T201FN1ST2010 T201FN1VT2010 1791 2017-12-26 00:00:00 854.69 0.87 1792 2017-12-26 00:20:00 855.76

Solution 1:

I think you need groupby + Grouper + some aggregate function:

df = last8.groupby(pd.Grouper(freq='2H', key='Time')).mean()

Or resample + some aggregate function:

df = last8.resample('2H', on='Time').mean()

Or use groupby + floor:

df = last8.groupby(last8.Time.dt.floor('2H')).mean()

print (df)
                     T201FN1ST2010  T201FN1VT2010
Time                                             
2017-12-26 00:00:00     855.551667       0.866667
2017-12-26 02:00:00     855.878333       0.853333
2017-12-26 04:00:00     855.725000       0.856667
2017-12-26 06:00:00     854.930000       0.880000

Post a Comment for "Groupby Every 2 Hours Data Of A Dataframe"