Python | Pandas dataframe.ffill()

01 Jul, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas dataframe.ffill() function is used to fill the missing value in the dataframe. ‘ffill’ stands for ‘forward fill’ and will propagate last valid observation forward. 
Syntax: DataFrame.ffill(axis=None, inplace=False, limit=None, downcast=None)Parameters: axis : {0, index 1, column} inplace : If True, fill in place. Note: this will modify any other views on this object, (e.g. a no-copy slice for a column in a DataFrame). limit : If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None. Downcast: a dict of item->dtype of what to downcast if possible, or the string ‘infer’ which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible)Returns : filled : DataFrame  
Example #1: Use ffill() function to fill the missing values along the index axis. Note : When ffill() is applied across the index then any missing value is filled based on the corresponding value in the previous row. 
Python3
# importing pandas as pdimport pandas as pd # Creating the dataframedf=pd.DataFrame({"A":[5,3,None,4],                 "B":[None,2,4,3],                 "C":[4,3,8,5],                 "D":[5,4,2,None]}) # Print the dataframedf
Let’s fill the missing value over the index axis 
Python3
# applying ffill() method to fill the missing valuesdf.ffill(axis = 0)
Output :  
Notice, values in the first row is still NaN value because there is no row above it from which non-NA value could be propagated.   Example #2: Use ffill() function to fill the missing values along the column axis. Note : When ffill is applied across the column axis, then missing values are filled by the value in previous column in the same row. 
Python3
# importing pandas as pdimport pandas as pd # Creating the dataframedf=pd.DataFrame({"A":[5,3,None,4],                 "B":[None,2,4,3],                 "C":[4,3,8,5],                 "D":[5,4,2,None]}) # Print the dataframedf
Let’s fill the missing value over the column axis 
Python3
# applying ffill() method to fill the missing valuesdf.ffill(axis = 1)
Output :  
Notice, the value in the first column is NaN value because there is no cell left to it and so this cell cannot be filled using the previous cell value along the column axis. 
amansingh110000
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts