if I have a dataset like-
JavaScript
x
7
1
Subject_ID DRUG LOS
2
2456 Syringe (Neonatal) *NS* 1.56
3
2456 Heparin 1.56
4
12345 Syringe (Neonatal) *NS* 0.78
5
12345 ampicillin 0.78
6
12345 gentamicin 0.78
7
As output, I want that the Drug name with the same Subject_ID will concatenate in one row.
JavaScript
1
5
1
Subject_ID DRUG LOS
2
2456 Syringe (Neonatal) *NS*, Heparin 1.56
3
12345 Syringe (Neonatal) *NS*, ampicillin, gentamicin 0.78
4
5
How can I do that in Python pandas?
Advertisement
Answer
Group the dataframe by Subject_ID
then call agg
with ', '.join
as aggregate for DRUG
column, and first
as aggregate for LOS
column.
JavaScript
1
7
1
>>> df.groupby(['Subject_ID']).agg({'DRUG':', '.join, 'LOS':'first'})
2
3
DRUG LOS
4
Subject_ID
5
2456 Syringe (Neonatal) *NS*, Heparin 1.56
6
12345 Syringe (Neonatal) *NS*, ampicillin, gentamicin 0.78
7