Table of Contents
Pandas has a function called fillna() which allows you to replace any NaN values in a DataFrame or Series with a specified value. To replace NaN values with zero in Pandas, you can use the fillna() function with the value parameter set to 0. This will replace all NaN values in the DataFrame or Series with zero. You can also use the replace() function in Pandas to replace the NaN values with zero, using the parameter value set to 0. This will replace all NaN values in the DataFrame or Series with zero.
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
#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