How can I replace NaN values with zero in Pandas?

How can I replace NaN values with zero in Pandas?

Replacing NaN values with zero in Pandas is a simple and efficient way to handle missing data in a dataset. This can be done by using the “fillna()” function in Pandas, which allows for the replacement of NaN values with a specified value, in this case zero. This method ensures that the data remains consistent and allows for easier analysis and manipulation. By replacing NaN values with zero, the data will be more accurate and reliable, making it easier to draw meaningful insights and conclusions. Additionally, this approach is widely used in data cleaning and preprocessing tasks, making it an essential skill for data analysts and scientists.

Replace NaN Values with Zero in Pandas


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

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

Cite this article

stats writer (2024). How can I replace NaN values with zero in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-replace-nan-values-with-zero-in-pandas/

stats writer. "How can I replace NaN values with zero in Pandas?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-replace-nan-values-with-zero-in-pandas/.

stats writer. "How can I replace NaN values with zero in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-replace-nan-values-with-zero-in-pandas/.

stats writer (2024) 'How can I replace NaN values with zero in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-replace-nan-values-with-zero-in-pandas/.

[1] stats writer, "How can I replace NaN values with zero in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I replace NaN values with zero in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top