I have a data frame which look like this:
ID Time Quantity Type
1 2019-04-03 05:51:54 0 Dog|Cat
2 2019-05-03 05:51:54 0 Dog
3 2019-04-02 05:51:54 0 Pig|Cat
4 2019-04-03 05:51:54 2
4 2019-07-03 04:51:54 0 Dog|Cat|Pig
3 2019-04-09 05:51:54 4
There is also a dictonry to sort the type column:
dictionary = {'Cat': 1,'Dog': 2,'Pig': 3}
I would like to group the table by ID and get the max for the other 3 columns. I have used this to group the first two:
df.groupby(['ID']).agg({'Time':'max','Quantity':'max'})
What I'm having trouble with is integrating the additional code to get the max value from the dictionary into the aggregate function. I think it should be something like this:
max(df['Type'].str.split(pat='|'), key=lambda s: dictionary[s])
My idea output would be:
ID Time Quantity Type
1 2019-04-03 05:51:54 0 Dog
2 2019-05-03 05:51:54 0 Dog
3 2019-04-09 05:51:54 4 Pig
4 2019-07-03 04:51:54 2 Pig
I'm sorry if I'm missing something elementary as I'm fairly new to using pandas