Q:

Grouping Pandas Dataframe with conditions

I have the following dataset :

I want to group the data based on userid,deviceid and ticketid. TicketStartTime should be the earliest entry for that Ticketid. TicketEndTime should be the latest entry for that TicketID. TicketStatus should be the one corresponding to the latest  TicketEndTime .
Desired Dataset :

A:

You can use agg method on the groupby object to select aggregations per column:
df.groupby(['UserID', 'DeviceID', 'TicketID']).agg({
    'TicketStatus': 'last',
    'TicketStartTime': 'first',
    'TicketEndTime': 'last'
}).reset_index()

