Python - How to fill NAN values with mean in Pandas?


For mean, use the mean() function. Calculate the mean for the column with NaN and use the fillna() to fill the NaN values with the mean.

Let us first import the required libraries −

import pandas as pd
import numpy as np

Create a DataFrame with 2 columns and some NaN values. We have entered these NaN values using numpy np.NaN −

dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'],"Units": [100, 150, np.NaN, 80, np.NaN, np.NaN]
   }
)

Finding mean of the column values with NaN i.e, for Units columns here. So, the Units column has 100, 150 and 80; therefore, the mean would be 110 −

meanVal = dataFrame['Units'].mean()

Replace NaNs with the mean of the column where it is located. The mean calculated above is 110, so NaN values will be replaced with 110 −

dataFrame['Units'].fillna(value=meanVal, inplace=True)

Example

Following is the code −

import pandas as pd
import numpy as np

# Create DataFrame
dataFrame = pd.DataFrame(
   {
      "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'],"Units": [100, 150, np.NaN, 80, np.NaN, np.NaN]
   }
)

print"DataFrame ...\n",dataFrame

# finding mean of the column values with NaN i.e, for Units columns here
# so the Units column has 100, 150 and 80; therefore the mean would ne 110
meanVal = dataFrame['Units'].mean()

# Replace NaNs with the mean of the column where it is located
# the mean calculated above is 110, so NaN values will be replaced with 110
dataFrame['Units'].fillna(value=meanVal, inplace=True)
print"\nUpdated Dataframe after filling NaN values with mean...\n",dataFrame

Output

This will produce the following output −

DataFrame ...
       Car   Units
0      BMW   100.0
1    Lexus   150.0
2    Lexus     NaN
3  Mustang    80.0
4  Bentley     NaN
5  Mustang     NaN

Updated Dataframe after filling NaN values with mean...
       Car   Units
0      BMW   100.0
1    Lexus   150.0
2    Lexus   110.0
3  Mustang    80.0
4  Bentley   110.0
5  Mustang   110.0

Updated on: 20-Sep-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements