How to replace NaN values of a series with the mean of elements using the fillna() method?

PandasServer Side ProgrammingProgramming

In the process of pandas data cleaning, replacing missing values takes a very important role and in some conditions we must replace those missing values with the mean of series elements. This can be done by using the fillna() method.

The basic operation of this pandas series.fillna() method is used to replace missing values (Nan or NA) with a specified value. Initially, the method verifies all the Nan values and replaces them with the assigned replacement value.

Example 1

Here we will see how the series.fillna() method replaces the missing value with mean.

# importing pandas package
import pandas as pd
import numpy as np

# create a series
s = pd.Series([69, np.nan, np.nan, 10, 30, 52, 70, np.nan, 54, 79, np.nan])
print(s)

# replace Missing values with an average value
result = s.fillna(s.mean())
print('Result:')
print(result)

Explanation

In the following example, we will replace the missing values with the average value of series elements. To do this, we applied the series.mean() function as a parameter to the fillna() method.

Output

The output is given below −

0    69.0
1     NaN
2     NaN
3    10.0
4    30.0
5    52.0
6    70.0
7     NaN
8    54.0
9    79.0
10    NaN
dtype: float64

Result:
0    69.0
1    52.0
2    52.0
3    10.0
4    30.0
5    52.0
6    70.0
7    52.0
8    54.0
9    79.0
10   52.0
dtype: float64

As you can see, the value 52.0 (average value) is replaced with Nan values in the index positions 1,2,5,7, and 10.

Example 2

Let’s take another example to replace missing values Nan/NA with an average value of the series elements.

# importing pandas package
import pandas as pd
import numpy as np

# create a series
s = pd.Series([np.nan, 49, 45, 47, 99, 99, np.nan, np.nan, 94, np.nan])
print(s)

# replace Missing values with an average value
result = s.fillna(s.mean())
print('Result:')
print(result)

Explanation

Initially, we have created a pandas series object with a list of integers and Nan values, then called the fillna() method with the mean value. By using the series.mean() function we calculated the average value and then applied that mean value as a parameter of the fillna() method.

Output

The output is given below −

0    NaN
1    49.0
2    45.0
3    47.0
4    99.0
5    99.0
6    NaN
7    NaN
8    94.0
9    NaN
dtype: float64

Result:
0    72.166667
1    49.000000
2    45.000000
3    47.000000
4    99.000000
5    99.000000
6    72.166667
7    72.166667
8    94.000000
9    72.166667
dtype: float64

The average value 72.166667 is replaced in place of missing values, the index positions are 0, 6, 7, and 9.

raja
Updated on 07-Mar-2022 07:31:02

Advertisements