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

How to merge rows and fill empty columns in python?

Name Age Gender Occupation
Joey 18
Joey Male
Joey Engineer

I tried groupby().first() but that doesn’t work
because it gives such a table:

Name Age Gender Occupation
Joey 18

But I want a table like this:

Name Age Gender Occupation
Joey 18 Male Engineer

I have table like this, and I want to merge all the rows by name and get age, gender and occupation in the same row.

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 :

You can replace '' with NaN, groupby "Name", then use first:

df = df.replace('', np.nan).groupby('Name').first().reset_index()

Output:

   Name   Age Gender Occupation
0  Joey  18.0   Male   Engineer
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