How can Pandas fillna() be used to replace NaN values in a dataset?

How can Pandas fillna() be used to replace NaN values in a dataset?

Pandas fillna() is a powerful method that allows for the replacement of missing or NaN (Not a Number) values in a dataset. This function takes in a variety of parameters, such as a single value, a dictionary, or a Series, to fill in the missing values with the desired data. It can be applied to a specific column or the entire dataset, making it a versatile tool for data cleaning and manipulation. By using Pandas fillna(), the NaN values in a dataset can be efficiently replaced with meaningful data, ensuring the accuracy and completeness of the data for further analysis.

Use Pandas fillna() to Replace NaN Values


You can use the fillna() function to replace NaN values in a pandas DataFrame.

This function uses the following basic syntax:

#replace NaN values in one column
df['col1'] = df['col1'].fillna(0)

#replace NaN values in multiple columns
df[['col1', 'col2']] = df[['col1', 'col2']].fillna(0) 

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

This tutorial explains how to use this function with the following pandas DataFrame:

import numpy as np
import pandas as pd

#create DataFrame with some NaN values
df = pd.DataFrame({'rating': [np.nan, 85, np.nan, 88, 94, 90, 76, 75, 87, 86],
                   'points': [25, np.nan, 14, 16, 27, 20, 12, 15, 14, 19],
                   'assists': [5, 7, 7, np.nan, 5, 7, 6, 9, 9, 5],
                   'rebounds': [11, 8, 10, 6, 6, 9, 6, 10, 10, 7]})

#view DataFrame
df

        rating	points	assists	rebounds
0	NaN	25.0	5.0	11
1	85.0	NaN	7.0	8
2	NaN	14.0	7.0	10
3	88.0	16.0	NaN	6
4	94.0	27.0	5.0	6
5	90.0	20.0	7.0	9
6	76.0	12.0	6.0	6
7	75.0	15.0	9.0	10
8	87.0	14.0	9.0	10
9	86.0	19.0	5.0	7

Example 1: Replace NaN Values in One Column

The following code shows how to replace the NaN values with zeros in the “rating” column:

#replace NaNs with zeros in 'rating' column
df['rating'] = df['rating'].fillna(0)

#view DataFrame 
df

	rating	points	assists	rebounds
0	0.0	25.0	5.0	11
1	85.0	NaN	7.0	8
2	0.0	14.0	7.0	10
3	88.0	16.0	NaN	6
4	94.0	27.0	5.0	6
5	90.0	20.0	7.0	9
6	76.0	12.0	6.0	6
7	75.0	15.0	9.0	10
8	87.0	14.0	9.0	10
9	86.0	19.0	5.0	7

Example 2: Replace NaN Values in Multiple Columns

The following code shows how to replace the NaN values with zeros in both the “rating” and “points” columns:

#replace NaNs with zeros in 'rating' and 'points' columns
df[['rating', 'points']] = df[['rating', 'points']].fillna(0)

#view DataFrame
df

	rating	points	assists	rebounds
0	0.0	25.0	5.0	11
1	85.0	0.0	7.0	8
2	0.0	14.0	7.0	10
3	88.0	16.0	NaN	6
4	94.0	27.0	5.0	6
5	90.0	20.0	7.0	9
6	76.0	12.0	6.0	6
7	75.0	15.0	9.0	10
8	87.0	14.0	9.0	10
9	86.0	19.0	5.0	7

Example 3: Replace NaN Values in All Columns

The following code shows how to replace the NaN values in every column with zeros:

#replace NaNs with zeros in all columns 
df = df.fillna(0)

#view DataFrame
df

        rating	points	assists	rebounds
0	0.0	25.0	5.0	11
1	85.0	0.0	7.0	8
2	0.0	14.0	7.0	10
3	88.0	16.0	0.0	6
4	94.0	27.0	5.0	6
5	90.0	20.0	7.0	9
6	76.0	12.0	6.0	6
7	75.0	15.0	9.0	10
8	87.0	14.0	9.0	10
9	86.0	19.0	5.0	7

You can find the complete online documentation for the fillna() function .

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

Cite this article

stats writer (2024). How can Pandas fillna() be used to replace NaN values in a dataset?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-pandas-fillna-be-used-to-replace-nan-values-in-a-dataset/

stats writer. "How can Pandas fillna() be used to replace NaN values in a dataset?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-pandas-fillna-be-used-to-replace-nan-values-in-a-dataset/.

stats writer. "How can Pandas fillna() be used to replace NaN values in a dataset?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-pandas-fillna-be-used-to-replace-nan-values-in-a-dataset/.

stats writer (2024) 'How can Pandas fillna() be used to replace NaN values in a dataset?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-pandas-fillna-be-used-to-replace-nan-values-in-a-dataset/.

[1] stats writer, "How can Pandas fillna() be used to replace NaN values in a dataset?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can Pandas fillna() be used to replace NaN values in a dataset?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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