
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
How Pandas Series ffill Method Works
The pandas series.ffill() method works equal to the series.fillna() with “method = ffill” function or we can say that the series.ffill() is a synonym for the forward fill method.
The series.ffill() method replaces the Nan or NA values in the given series object using the forward fill method. The parameters for this method are inplace, axis, limit, and downcast.
It does not have parameters like value and method. Because it takes the series element as a replacement value and fills the missing values using the forward fill method.
Example 1
In this following example, we have applied the ffill() method to the series object which is having object data with some Nan values and the default parameters are not changed.
# importing pandas package import pandas as pd import numpy as np # create a series s = pd.Series(["a", np.nan, np.nan, "b", "c", np.nan, "d", "e"]) print(s) # replace Missing values result = s.ffill() print('Result:') print(result)
Output
The output is given below −
0 a 1 NaN 2 NaN 3 b 4 c 5 NaN 6 d 7 e dtype: object Result: 0 a 1 a 2 a 3 b 4 c 5 c 6 d 7 e dtype: object
In the above output block, we can notice that the missing values of series objects are successfully updated with the previous row value.
Example 2
The following example has specified the inplace parameter equal to True so that the modifications are applied to the original series object.
# importing pandas package import pandas as pd import numpy as np # create a series s = pd.Series([np.nan, np.nan, 27, 61, np.nan, 93, np.nan, 68, 70, np.nan]) print(s) # replace Missing values s.ffill(inplace=True) print('Result:') print(s)
Explanation
If you don’t change the inplace parameter value from the default one then it will create a new series with updated values as a result, and the default value of this inplace parameter is False.
Output
The output is given below −
0 NaN 1 NaN 2 27.0 3 61.0 4 NaN 5 93.0 6 NaN 7 68.0 8 70.0 9 NaN dtype: float64 Result: 0 NaN 1 NaN 2 27.0 3 61.0 4 61.0 5 93.0 6 93.0 7 68.0 8 70.0 9 70.0 dtype: float64
We can notice that the Nan values at index position 0 and 1 remain the same, which is due to the fact that no previous value is available to do the forward fill operation.