Replace NaN with an empty string in DataFrame in Pandas

This tutorial will discuss about different ways to replace NaN with an empty string in a Pandas DataFrame.

Table Of Contents

Preparing DataSet

Let’s create a DataFrame with four columns, and six rows. This DataFrame will certain NaN values.

import pandas as pd
import numpy as np

# List of Tuples
employees= [('Suse',   'US',    'Tech',    5),
            ('Aadi',   'India', 'Tech' ,   7),
            (np.NaN,   np.NaN,  'PMO' ,    11),
            ('Shreya', 'India', 'Design',  2),
            (np.NaN,   'US',     np.NaN,   11),
            ('Sim',    np.NaN,    np.NaN,   4)]

# Create a DataFrame object from list of tuples
df = pd.DataFrame(employees,
                  columns=['Name', 'Location', 'Team', 'Experience'])

print(df)

Output

     Name Location    Team  Experience
0    Suse       US    Tech           5
1    Aadi    India    Tech           7
2     NaN      NaN     PMO          11
3  Shreya    India  Design           2
4     NaN       US     NaN          11
5     Sim      NaN     NaN           4

Now we want to replace the NaN values in all the columns of this DataFrame with the an empty string. There are different ways to do this. Let’s discuss them.

Replace NaN with an empty string using fillna()

In Pandas, a DataFrame has a function fillna(replacement_value), to replace all NaN values in the DataFrame with the given replacement_value. To replace all NaNs with an empty string, call the fillna() function, and pass an empty string as the value parameter in it. Also, pass the inplace=True as the second argument in the fillna(). It will modify the DataFrame in place.

# Replace NaN with an empty string in DataFrame
df.fillna(value="", inplace=True)

print(df)

Output

     Name Location    Team  Experience
0    Suse       US    Tech           5
1    Aadi    India    Tech           7
2                      PMO          11
3  Shreya    India  Design           2
4               US                  11
5     Sim                            4

It replaced all the NaN values with an empty string in the whole DataFrame.

Replace NaN with an empty string using replace()

Pandas DataFrame provides a function replace(value, replacement_value), to replace all the occurrences of a given value with a replacemenet value. To replace all occurrences of NaN with an empty string, pass both the NaN value and an empty string as arguments in the replace() function. Also, pass inplace as True, due to which all modifications in DataFrame will be in place.

# Replace NaN with an empty string in DataFrame
df.replace(np.NaN,  "", inplace=True)

print(df)

Output

     Name Location    Team  Experience
0    Suse       US    Tech           5
1    Aadi    India    Tech           7
2                      PMO          11
3  Shreya    India  Design           2
4               US                  11
5     Sim                            4

It replaced all the NaN values with an empty string in the whole DataFrame.

Summary

We learned two different ways to replace NaN with an empty string in the complete DataFrame in Pandas.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top