I have a table that looks like this:
P_id S_id Time
1 20 A 15
2 30 B 50
3 50 A 99
4 70 A 60
I want to group the table, based on the column "Sid", and sorted by Column "Time" so it will look like this:
P_id S_id
1 20,70,50 A
2 30 B
What is the best way to do this?
50instead of99in P_id column of the expected output.df = df.sort_values('Time') .groupby('S_id', sort=False)['P_id'].agg(lambda x: ','.join(x.astype(str))).reset_index()