Replace NaN with 0 in Pandas DataFrame

This tutorial will discuss about different ways to replace nan with 0 in pandas dataframe.

Table Of Contents

Preparing DataSet

First we will create a DataFrame, which has 3 columns, and six rows. This DataFrame has certain NaN values.

import pandas as pd
import numpy as np

# List of Tuples
players = [ ('Mark',   123,     789),
            ('Riti',   np.NaN,  np.NaN),
            ('Shanky', 132,     np.NaN),
            ('Shreya', 789,     np.NaN),
            ('Aadi',   np.NaN,  666),
            ('Sim',    890,     np.NaN)]

# Create a DataFrame object from list of tuples
df = pd.DataFrame(players,
                  columns=['Name', 'Level_1 Score', 'Level_2 Score'])

print(df)

Output

     Name  Level_1 Score  Level_2 Score
0    Mark          123.0          789.0
1    Riti            NaN            NaN
2  Shanky          132.0            NaN
3  Shreya          789.0            NaN
4    Aadi            NaN          666.0
5     Sim          890.0            NaN

Now we want to replace NaN values in all columns of this DataFrame with the value zero. There are different ways to do this. Let’s discuss them one by one.

Replace NaN with zero using fillna()

DataFrame in Pandas, provides a function fillna(value), to replace all NaN values in the DataFrame with the given value. To replace all NaNs with zero, call the fillna() function, and pass 0 in it, as the first argument. Also, pass the inplace=True as the second argument in the fillna(). This function will modify the DataFrame in place.

# Replace NaN with 0 in whole DataFrame
df.fillna(0, inplace=True)

print(df)

Output

     Name  Level_1 Score  Level_2 Score
0    Mark          123.0          789.0
1    Riti            0.0            0.0
2  Shanky          132.0            0.0
3  Shreya          789.0            0.0
4    Aadi            0.0          666.0
5     Sim          890.0            0.0

It replaced all the NaN values with zero in all the columns of DataFrame.

Replace NaN with zero using replace()

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

# Replace NaN with 0 in whole DataFrame
df.replace(np.NaN, 0, inplace=True)

print(df)

Output

     Name  Level_1 Score  Level_2 Score
0    Mark          123.0          789.0
1    Riti            0.0            0.0
2  Shanky          132.0            0.0
3  Shreya          789.0            0.0
4    Aadi            0.0          666.0
5     Sim          890.0            0.0

It replaced all the NaN values with zero in all the columns of DataFrame.

Summary

We learned two different ways to replace NaN with 0 in 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