Home > database >  Calculate time difference based on unique entries
Calculate time difference based on unique entries

Time:10-18

I have the following data frame:

Trip_nr Time1 Time2
1       08:00 05:17
1       08:05 05:18
1       08:40 05:19
2       10:42 08:10
2       10:50 08:11
3       21:00 16:23
3       21:04 16:24
3       21:07 16:25
3       21:09 16:26

For each Trip_nr I want to generate the following output:

  1. find earliest Time1
  2. find latest Time1
  3. find earliest Time2
  4. find latest Time2

I then want to show the output in the following format (also data frame)

unique_trip_nr earliest_Time1 latest_Time1 earliest_Time2 latest_Time2
1              08:00          08:40        05:17          05:19
2              10:42          10:50        08:10          08:11
3              21:00          21:09        16:23          16:26

CodePudding user response:

Use named aggregation:

df = df.groupby('Trip_nr', as_index=False).agg(earliest_Time1=('Time1','min'),
                                               latest_Time1=('Time1','max'),
                                               earliest_Time2=('Time2','min'),
                                               latest_Time2=('Time2','max'))
print (df)
   Trip_nr earliest_Time1 latest_Time1 earliest_Time2 latest_Time2
0        1          08:00        08:40          05:17        05:19
1        2          10:42        10:50          08:10        08:11
2        3          21:00        21:09          16:23        16:26
  • Related