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

Getting a blank result pandas

Python 3.9 and Pandas 1.3.4

So here is my dataframe that I’m working with:

First name  Last Name
Freddie     Mercury
John        Lennon
David       Bowie
Joseph
            Jovi

I would like a result of df["Full name"] = df["First name"] + df["Last name"] to produce a result even if it does not have both the first and last name column filled.

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

so df["Full name"]=

Full name
Freddie Mercury
John Lennon
David Bowie
Joseph
Jovi

This is my current code and does not produce a result if either a first name or last name is missing:

import pandas as pd

df = pd.read_csv('file.csv', dtype=str, header=0)

df["Full name"] = df["First name"] + " " + df["Last name"]

df.to_csv('file.csv', index=False)

This is currently producing:

Full name
Freddie Mercury
John Lennon
David Bowie


>Solution :

Pandas Fill Nan Values With Empty String

You can replace the nan values with a blank string so that it will include them when running your concatenation program. For example:

df.fillna("")

Include this just after you have read from your file and before you use your code. This can also be applied to individual columns.

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