Home > Net >  Getting a blank result pandas
Getting a blank result pandas

Time:11-23

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.

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


CodePudding user response:

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.

  • Related