Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combing Rows in a Single Dataframe

I have a dataframe that looks like this, where there is a new row per ID if one of the following columns has a value. I’m trying to combine on the ID, and just consolidate all of the remaining columns. I’ve tried every groupby/agg combination and can’t get the right output. There are no conflicting column values. So for instance if ID "1" has an email value in row 0, the remaining rows will be empty in the column. So I just need it to sum/consolidate, not concatenate or anything.

my current dataframe:
enter image description here

the output i’m looking to achieve:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

# fill Nones in string columns with empty string
df[['email', 'status']] = df[['email', 'status']].fillna('')

df = df.groupby('id').agg('max')

If you still want the index as you shown in desired output,

df = df.reset_index(drop=False)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading