How to change nan values to zero in pandas DataFrames?

Use the following code to replace empty values with zeros in a complete pandas DataFrame, one column or multiple ones:

# entire DataFrame

your_df.replace([np.nan, pd.NA,'', None], 0)

# one column
your_df['your_col'].replace([np.nan, pd.NA,'', None], 0)

#multiple columns
your_df[['your_col1', 'your_coln']].replace([np.nan, pd.NA,'', None], 0)

Example DataFrame

We’ll import Data Analysis libraries pandas and numpy. Numpy will be used to generate a empty values in the Python lists that we will use in order to build your DataFrame.

import pandas as pd
import numpy as np

interviewed = [32, np.nan, 49, 40, np.nan]
hired = [6, pd.NA, 7, 5, 9]
dates = pd.date_range (start='1/29/2023', periods = 5, freq = 'B' )

We will now construct our DataFrame:

hiring = pd.DataFrame (dict (dates = dates, interviewed = interviewed, hired = hired))

And look into its values:


Here is the data:


Replace empty nan values with zero in DataFrame columns

We will use the following code to change all empty values to zero. Note that we pass a list of possible empty values to the replace method.

hiring = hiring.replace([np.nan, pd.NA,'', None], 0)

Here’s our DataFrame:


Change nan to 0 in a specific pandas column

We are able also to selectively modify empty values in one or multiple columns of our DataFrame:

hiring['interviewed'].replace([np.nan, pd.NA,'', None], 0)

This will return the following series:

0    32.0
1     0.0
2    49.0
3    40.0
4     0.0
Name: interviewed, dtype: float64

Modify nans in multiple DataFrame columns

In the same way we can replace empty values in several columns:

hiring[['interviewed','hired']].replace([np.nan, pd.NA,'', None], 0)

Replace empty values by a string

In this case we’ll swap the empty values with the string value None

hiring[['interviewed','hired']].replace([np.nan, pd.NA,'', None], 'None')

Additional learning

How to change column values in Pandas DataFrames?