We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

17
996.405,05

Cart

Replace NaN with zero in a column in Pandas

This tutorial will discuss about different ways to replace NaN with zero in a column in pandas.

Table Of Contents

Introduction

Suppose we have a DataFrame,

Advertisements

     Name  Level_1 Scrore  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

We want to replace all NaN values in one column of this DataFrame i.e. in column Level_2 Scrore only. Like this,

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

There are different ways to do this. Let’s diccuss the one by one.

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 all NaN values in the column ‘Level_2 Score’ of this DataFrame with the value zero. Let’s see how to do this.

Method 1: Using fillna()

Syntax is:

df['column_name'].fillna(value=0, inplace=True)

Select a column of DataFrame using [] operator i.e. df[‘column_name’]. Then call the fillna() function on it, and pass following arguments in it,
* 0 as the first argument.
* inplace=True as the second argument

It will replace all the NaN values in given column with zero. Also, it will modify the selected DataFrame column in place.

Let’s see an example,

# replace all NaN values in Column 'Level_2 Score' with zero
df['Level_2 Score'].fillna(value=0, inplace=True)

print(df)

Output

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

All NaN values in column ‘Level_2 Score’ are replaced by 0.

Method 2: Using replace()

Syntax is:

df['column_name'].replace(np.NaN, 0, inplace=True)

Select the DataFrame column as series. The Series object in Pandas provides a function replace(), to replace all the occurrences of a given value in that series, with a replacemenet value.

To replace all occurrences of NaN with 0 in selected column, pass them as arguments to the replace() function. Also, pass inplace as True, due to which all modifications in the selected column will be done, in place.

# replace all NaN values in Column 'Level_2 Score' with zero
df['Level_2 Score'].replace(np.NaN, 0, inplace=True)

print(df)

Output

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

All NaN values in column ‘Level_2 Score’ are replaced by 0.

Summary

We learned about different ways to replace NaN values with zeros in a Pandas Columns. Thanks.

17
996.405,05

Cart