Skip to content Skip to sidebar Skip to footer

Finding Daily Maximum And Its Time-stamp (yyyy:mm:dd Hh:mm:ss) In Python Pandas

I actually have a 150 MB data of daily minute-wise measurements for everyday for two years. I have given here a sample data. I want to create a new data frame with maximum of each

Solution 1:

You can use GroupBy.transform or Resampler.transform for return max values in new Series and compare with original column:

df['DateTime'] = pd.to_datetime(df['DateTime'])
s = df.groupby(pd.Grouper(key='DateTime', freq='D'))['Power'].transform('max')
#alternative
#s = df.resample('D', on='DateTime')['Power'].transform('max')
df = df[df['Power'] == s]
print (df)
              DateTime       Power
4  2016-08-01 10:46:00  233.273915
8  2016-08-02 09:44:00  231.256910
13 2016-08-03 10:10:00  231.682716

Or create DatetimeIndex and add column after groupby for check idxmax:

df['DateTime'] = pd.to_datetime(df['DateTime'])
df = df.set_index('DateTime')
df = df.loc[df.groupby(pd.Grouper(freq='D'))['Power'].idxmax()]
print (df)
                          Power
DateTime                       
2016-08-01 10:46:00  233.273915
2016-08-02 09:44:00  231.256910
2016-08-03 10:10:00  231.682716

Solution of @Jon Clements, thank you:

df = (df.sort_values('Power')
        .groupby(df.DateTime.dt.to_period('D'))
        .last()
        .reset_index(drop=True))

Post a Comment for "Finding Daily Maximum And Its Time-stamp (yyyy:mm:dd Hh:mm:ss) In Python Pandas"