Skip to content
Advertisement

How to concate a column’s sentences for same subject id in a single row in python dataframe?

if I have a dataset like-

JavaScript

As output, I want that the Drug name with the same Subject_ID will concatenate in one row.

JavaScript

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
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement