4.5 C
London
Thursday, December 19, 2024
HomePandas in PythonDataFrame Functions in PythonPandas: How to Fill NaN Values with Mean (3 Examples)

Pandas: How to Fill NaN Values with Mean (3 Examples)

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 fillna() function to replace NaN values in a pandas DataFrame.

Here are three common ways to use this function:

Method 1: Fill NaN Values in One Column with Mean

df['col1'] = df['col1'].fillna(df['col1'].mean())

Method 2: Fill NaN Values in Multiple Columns with Mean

df[['col1', 'col2']] = df[['col1', 'col2']].fillna(df[['col1', 'col2']].mean())

Method 3: Fill NaN Values in All Columns with Mean

df = df.fillna(df.mean())

The following examples show how to use each method in practice 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: Fill NaN Values in One Column with Mean

The following code shows how to fill the NaN values in the rating column with the mean value of the rating column:

#fill NaNs with column mean in 'rating' column
df['rating'] = df['rating'].fillna(df['rating'].mean())

#view updated DataFrame 
df

	rating	points	assists	rebounds
0	85.125	25.0	5.0	11
1	85.000	NaN	7.0	8
2	85.125	14.0	7.0	10
3	88.000	16.0	NaN	6
4	94.000	27.0	5.0	6
5	90.000	20.0	7.0	9
6	76.000	12.0	6.0	6
7	75.000	15.0	9.0	10
8	87.000	14.0	9.0	10
9	86.000	19.0	5.0	7

The mean value in the rating column was 85.125 so each of the NaN values in the rating column were filled with this value.

Example 2: Fill NaN Values in Multiple Columns with Mean

The following code shows how to fill the NaN values in both the rating and points columns with their respective column means:

#fill NaNs with column means in 'rating' and 'points' columns
df[['rating', 'points']] = df[['rating', 'points']].fillna(df[['rating', 'points']].mean())

#view updated DataFrame
df

	rating	points	assists	rebounds
0	85.125	25.0	5.0	11
1	85.000	18.0	7.0	8
2	85.125	14.0	7.0	10
3	88.000	16.0	NaN	6
4	94.000	27.0	5.0	6
5	90.000	20.0	7.0	9
6	76.000	12.0	6.0	6
7	75.000	15.0	9.0	10
8	87.000	14.0	9.0	10
9	86.000	19.0	5.0	7

The NaN values in both the ratings and points columns were filled with their respective column means.

Example 3: Fill NaN Values in All Columns with Mean

The following code shows how to fill the NaN values in each column with the column means:

#fill NaNs with column means in each column 
df = df.fillna(df.mean())

#view updated DataFrame
df

        rating	points	assists	  rebounds
0	85.125	25.0	5.000000  11
1	85.000	18.0	7.000000  8
2	85.125	14.0	7.000000  10
3	88.000	16.0	6.666667  6
4	94.000	27.0	5.000000  6
5	90.000	20.0	7.000000  9
6	76.000	12.0	6.000000  6
7	75.000	15.0	9.000000  10
8	87.000	14.0	9.000000  10
9	86.000	19.0	5.000000  7

Notice that the NaN values in each column were filled with their column mean.

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

Additional Resources

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

How to Count Missing Values in Pandas
How to Drop Rows with NaN Values in Pandas
How to Drop Rows that Contain a Specific Value 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