0

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?

oren_isp
  • 729
  • 1
  • 7
  • 22

1 Answers1

0

You can try this. df here is the name of your data frame

import pandas as pd
df2 = pd.DataFrame({'S_id': ['A', 'B']})
df2.loc[:,'P_id'] = ''
for letter in df2.S_id.unique():
    indx = df2.loc[df2['S_id']==letter].index.values
    df1 = df.sort_values(by = ['S_id' ,'Time'])
    array_values = list(df1[df1.S_id ==letter].P_id.values)#.astype(object)
    df2.at[indx[0], 'P_id'] = array_values
df2
Jorge
  • 2,181
  • 1
  • 19
  • 30