Home > database >  Pandas get max DateTime per day
Pandas get max DateTime per day

Time:09-27

Team, I have a simple dataframe with 2 columns(Datetime, Duration), Need to find what is the value of Duration on max Datetime value for each day. Below is the sample data for the dataframe

starttime             Duartion
2021-07-17 03:30:00   1234
2021-07-17 07:30:00   498
2021-07-18 03:30:00   1700
2021-07-18 07:40:00   2185

o/p

starttime             Duartion
2021-07-17 07:30:00   498
2021-07-18 07:40:00   2185

Any help on this please.

CodePudding user response:

Pre-requisite, using datetime type for "starttime":

df['starttime'] = pd.to_datetime(df['starttime'])

If the "starttime" values are already sorted:

df.groupby(df['starttime'].dt.normalize(), as_index=False).last()

else:

df.loc[df.groupby(df['starttime'].dt.normalize())['starttime'].idxmax().values]

CodePudding user response:

This will take the maximum value per day:

df.set_index('starttime').resample('1D').max()
  • Related