7.7 C
London
Sunday, March 9, 2025
HomePandas in PythonDataFrame Functions in PythonHow to Replace NaN Values with Zero in Pandas

How to Replace NaN Values with Zero in Pandas

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories