
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use Pandas Series fillna to Replace Missing Values
The pandas series.fillna() method is used to replace missing values with a specified value. This method replaces the Nan or NA values in the entire series object.
The parameters of pandas fillna are as follows −
Value − it allows us to specify a particular value to replace Nan’s, by default it takes None.
Method − it is used to fill the missing values in the reindexed Series. It takes any of these values like ‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, and None(default).
Inplace − this parameter takes a boolean value. If it takesTrue, then the modifications are applied to the original series object itself, otherwise, it will create a new series with updated missing values as result. The default value is False.
Limit − This parameter takes an integer value, which is used to specify how many NA values you wish to fill forward/backward. The default value of this parameter is None.
Axis − it takes 0 or index label.
Downcast − It takes a dictionary which specifies the downcast of data types.
Here we will see how the series.fillna() method replaces the missing value.
Example 1
In the following example, we will replace the missing values with an integer value 10.
# importing pandas package import pandas as pd import numpy as np # create a series s = pd.Series([np.nan, np.nan, 89, 64, np.nan], index=["a", "b", "c", "d", "e"]) print(s) # replace Missing values with 10 result = s.fillna(10) print('Result:') print(result)
Explanation
Initially, we have created the pandas series object with some missing values. Then applied the fillna() method with value 10. Here, the default parameters are not changed.
Output
The output is as follows −
a NaN b NaN c 89.0 d 64.0 e NaN dtype: float64 Result: a 10.0 b 10.0 c 89.0 d 64.0 e 10.0 dtype: float64
In the above output block, we can see that all Nan values in the entire series object are nicely being replaced with value 10.
Example 2
This time, we will replace the missing values by specifying the bfill value to the method parameter. So that, we do not need to specify any particular value to fill the missing values, it will take the value after Nan for replacement.
# importing pandas package import pandas as pd import numpy as np # create a series s = pd.Series([np.nan, np.nan, 89, 64, np.nan], index=["a", "b", "c", "d", "e"]) print(s) # replace Missing values with bfill result = s.fillna(method='bfill') print('Result:') print(result)
Output
The output is given below −
a NaN b NaN c 89.0 d 64.0 e NaN dtype: float64 Result: a 89.0 b 89.0 c 89.0 d 64.0 e NaN dtype: float64
In the above output block, We can see that Nan values at index positions a, b are replaced with a value 89, which is due to the fact that we have mentioned the bfill value to the method parameter. The Nan value at index position e remains the same.