Home » How to Replace NaN Values with Zero in Pandas

How to Replace NaN Values with Zero in Pandas

by Tutor Aspire

You can use the following methods to replace NaN values with zeros in a pandas DataFrame:

Method 1: Replace NaN Values with Zero in One Column

df['col1'] = df['col1'].fillna(0)

Method 2: Replace NaN Values with Zero in Several Columns

df[['col1', 'col2']] = df[['col1', 'col2']].fillna(0)

Method 3: Replace NaN Values with Zero in All Columns

df = df.fillna(0)

The following examples show how to use each of these methods with the following pandas DataFrame:

import pandas as pd
import numpy as np

#create DataFrame
df = pd.DataFrame({'points': [25, np.nan, 15, 14, 19, 23, 25, 29],
                   'assists': [5, np.nan, 7, np.nan, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, np.nan, 9, np.nan]})

#view DataFrame
print(df)

   points  assists  rebounds
0    25.0      5.0      11.0
1     NaN      NaN       8.0
2    15.0      7.0      10.0
3    14.0      NaN       6.0
4    19.0     12.0       6.0
5    23.0      9.0       NaN
6    25.0      9.0       9.0
7    29.0      4.0       NaN

Method 1: Replace NaN Values with Zero in One Column

The following code shows how to replace NaN values with zero in just the ‘assists’ column:

#replace NaN values with zero in 'assists' column
df['assists'] = df['assists'].fillna(0)

#view updated DataFrame
print(df)

   points  assists  rebounds
0    25.0      5.0      11.0
1     NaN      0.0       8.0
2    15.0      7.0      10.0
3    14.0      0.0       6.0
4    19.0     12.0       6.0
5    23.0      9.0       NaN
6    25.0      9.0       9.0
7    29.0      4.0       NaN

Notice that the NaN values in the ‘assists’ column have been replaced with zeros, but the NaN values in every other column still remain.

Method 2: Replace NaN Values with Zero in Several Columns

The following code shows how to replace NaN values with zero in the ‘points’ and ‘assists’ columns:

#replace NaN values with zero in 'points' and 'assists' column
df[['points', 'assists']] = df[['points', 'assists']].fillna(0)

#view updated DataFrame
print(df)

   points  assists  rebounds
0    25.0      5.0      11.0
1     0.0      0.0       8.0
2    15.0      7.0      10.0
3    14.0      0.0       6.0
4    19.0     12.0       6.0
5    23.0      9.0       NaN
6    25.0      9.0       9.0
7    29.0      4.0       NaN

Method 3: Replace NaN Values with Zero in All Columns

The following code shows how to replace NaN values with zero in every column of the DataFrame:

#replace NaN values with zero in all columns
df = df.fillna(0)

#view updated DataFrame
print(df)

   points  assists  rebounds
0    25.0      5.0      11.0
1     0.0      0.0       8.0
2    15.0      7.0      10.0
3    14.0      0.0       6.0
4    19.0     12.0       6.0
5    23.0      9.0       0.0
6    25.0      9.0       9.0
7    29.0      4.0       0.0

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Replace Specific Values in Pandas
How to Filter a Pandas DataFrame by Column Values
How to Fill NA Values for Multiple Columns in Pandas

You may also like