0

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.

Prakash
  • 281
  • 5
  • 18
  • have a look at https://stackoverflow.com/questions/15705630/get-the-rows-which-have-the-max-value-in-groups-using-groupby – balderman Sep 27 '21 at 13:48

2 Answers2

1

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]
mozway
  • 194,879
  • 13
  • 39
  • 75
0

This will take the maximum value per day:

df.set_index('starttime').resample('1D').max()
vtasca
  • 1,660
  • 11
  • 17