- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to 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.
- Related Articles
- How to replace NaN values of a series with the mean of elements using the fillna() method?
- How to replace NaN values by Zeroes in a column of a Pandas Series?
- How to compare two DataFrames in Python Pandas with missing values
- How to replace missing values with median in an R data frame column?
- How to replace missing values with linear interpolation method in an R vector?
- How to replace missing values with row means in an R data frame?
- How to merge two unequal data frames and replace missing values to 0 in R?
- How to change index values of a Pandas series after creation?
- How to get the index and values of series in Pandas?
- How to add two pandas Series objects by handling None values?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- Python Pandas - Fill missing columns values (NaN) with constant values
- How does pandas series argsort handles nan values?
- How to use the series.isin() method to check values in a series?
- Python Pandas - Filling missing column values with mode
