Home »
Python »
Python Programs
Pandas DataFrame forward fill method (pandas.DataFrame.ffill())
Learn about the Pandas DataFrame.ffill() method, its usages, explanation, and examples.
By Pranit Sharma Last updated : September 29, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Python pandas.DataFrame.ffill() Method
To forward fill pandas DataFrame, we use a method provided by pandas called DataFrame.ffill(). Pandas DataFrame.ffill() method is used to fill the missing value in the DataFrame. "ffill" stands for "forward fill" and will forward last valid observation.
Syntax
The syntax of DataFrame.ffill() method is:
DataFrame.ffill(
axis=None,
inplace=False,
limit=None,
downcast=None
)
Parameter(s)
The parameters of DataFrame.ffill() method are:
- axis: 0 for index and 1 for column
- inplace: used to define whether the modification takes in place or a copy is made.
- limit: max number of forward nan values to be filled.
- downcast: a dictionary of items in form of data types in case we need to typecast them.
Python pandas.DataFrame.ffill() Method Example
# Importing pandas package
import pandas as pd
# Importing methods from sklearn
from sklearn.preprocessing import MinMaxScaler
# Creatinging a dictionary
d = {
'A':['Tiger','Lion','Leopard',None],
'B':['Dog',None,'Wolf','Fox'],
'C':['Peacock','Crow',None,'Parrot']
}
# Creating DataFrame
df = pd.DataFrame(d)
# Display the DataFrame
print("Original DataFrame:\n",df,"\n")
# applying ffill() method
result = df.ffill(axis = 0)
# Display result
print("Result:\n",result)
Output
The output of the above program is:
Reference: pandas.DataFrame.ffill()
Python Pandas Programs »