Pandas Series function ffill() in Python
The Pandas Service function ffill() used to forward fill the missing values with specified methods or None if inplace = True. It returns Series object that is forward filled with missing value or None.
Syntax
 1 Series.ffill(*, axis = None, inplace = False, limit = None, downcast = None)
axis : It specifies the axis value that fills the value on specified axis. The value can be either 0 or index. If not specified the default value will be None.
inplace : It is a boolean value that specifies whether to create new object or modify a source object.
limit : It is an integer value that specifies the maximum number of consecutive NaN values either forward or backward to fill. if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If not specified an entire axis where NaNs will be filled.
downcast : It is a dictionary that specifies what to downcast if possible or the string infer which will try to downcast to appropriate equal type.
ffill() function
 1 import pandas as pd
 2 import numpy as np
 3 
 4 ser = pd.Series([16, 17, np.nan, np.nan, 20])
 5 
 6 res = ser.ffill()
 7 print('The forward filled Series object :')
 8 print(res)
In the above example, a Series object is created by passing an array that includes nan values. The ffill() function is called that forward fill the nan value. If an element value is nan, it will be filled with previous element value. The result is assigned to the variable that will be printed on console.
Output
 1 The forward filled Series object :
 2 0    16.0
 3 1    17.0
 4 2    17.0
 5 3    17.0
 6 4    20.0
 7 dtype: float64

Series with string literals

Series with string literals
 1 import pandas as pd
 2 import numpy as np
 3 
 4 ser = pd.Series(['welcome', 'to', np.nan, 'IOGyan', np.nan])
 5 
 6 res = ser.ffill()
 7 print('The forward filled Series object :')
 8 print(res)
In the above example, a Series object is created by passing an array of string literals that includes nan values. The ffill() function is called that forward fill the nan value. If an element value is nan, it will be filled with previous element value. The result is assigned to the variable that will be print.
Output
 1 The forward filled Series object :
 2 0    welcome
 3 1         to
 4 2         to
 5 3     IOGyan
 6 4     IOGyan
 7 dtype: object

Series with first element as NaN

Series with first element as NaN
 1 import pandas as pd
 2 import numpy as np
 3 
 4 ser = pd.Series([np.nan, 10, 20, np.nan, 30, np.nan])
 5 
 6 res = ser.ffill()
 7 print('The forward filled Series object :')
 8 print(res)
In the above example, a Series object is created by passing an array of number where first element value is nan and at other index position. The ffill() function is called that forward fill the nan value. If a first element value is nan, it will remain as nan and for other element will be filled with previous element value. The result is assigned to the variable that will be print.
Output
 1 The forward filled Series object :
 2 0     NaN
 3 1    10.0
 4 2    20.0
 5 3    20.0
 6 4    30.0
 7 5    30.0
 8 dtype: float64
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us