This tutorial will explore the Python pandas DataFrame.ffill() method. This method adds the missing value to the DataFrame by filling it from the last value before the null value. Fill stands for “forward fill.” By using this method on the DataFrame and learning the syntax and parameters, we will be in a position to solve examples and comprehend the DataFrame.ffill() function.
Python’s fantastic ecosystem of data-centric python packages makes it a wonderful language for conducting data analysis. One such tool, Pandas, greatly simplifies importing and analyzing data.
The dataframe’s missing value is filled using the dataframe.ffill() method of Pandas.ffill, which stands for “forward fill,” propagates the most recent valid observation.
ffill syntax
The syntax is as follows:
axis_var =None inplace_var=False limit_var=None downcast=None DataFrame.ffill(axis=axis_var, inplace=inplace_var, limit=limit_var, downcast=downcast)
or simply
dataframe.ffill(axis, inplace, limit, downcast)
Parameters
The parameters for the DataFrame.ffill include:
axis
{0, index 1, column}
inplace
Fill in the blanks if true. Additionally, note that this modifies all prior interpretations of this object, for example, a no-copy slice for a column in a DataFrame.
Limit
The count of maximum successive NaN values to forward- or backward-fill if the method is provided. In other words, the gap will only be partially filled if more than this many consecutive NaNs exist. It represents the highest count of entries along the axis where NaNs will be filled if the method is not provided. If None, then it must be greater than 0.
Downcast
If downcasting is available, use a dictionary of item->dtype or the string “infer,” which will attempt to downcast to an equal suitable type, e.g., float64 to int64, if possible.
Returns: filled: DataFrame
Example: Using the DataFrame.ffill() Method to fill in missing values
The missing values along the chosen axis are filled using the DataFrame.ffill() method. The example below demonstrates the same.
# start by importing pandas library with an alias pd import pandas as pd # secondly, import the numpy library with an alias np import numpy as np #making a DataFrame with empty values data_frame=pd.DataFrame({"Q":[4,None,6],"R":[None,6,np.nan],"S":[6,2.25,np.nan],"T":[11,6,None]}) print("---The provided DataFrame is------") print(data_frame) print("----Making up for missing values --------") print(data_frame.ffill())
Example: Fill in the missing numbers along the index axis using the ffill() method
Any missing value is filled using the equivalent value from the preceding row when ffill() is run across the index.
# importing pandas as pd import pandas as pd # Creating the dataframe data_frame=pd.DataFrame({"Q":[7,5,None,6], "R":[None,4,6,5], "S":[6,5,6,7], "T":[7,6,4,None]}) # Print the dataframe print(data_frame)
Fill the empty space on the index axis.
# applying the ffill() method to fill the missing values data_frame.ffill(axis = 0)
Because no row exists above it from which a non-NA value might spread, the values in the first row are still NaN values.
Example: Fill in the missing values along the column axis using the ffill() method
The value from the previous column in the same row is used to fill in any missing values when ffill is applied across the column axis.
# first, import the pandas' library as pd import pandas as pd # dataframe's creation data_frame=pd.DataFrame({"Q":[7,5,None,6], "R":[None,4,6,5], "S":[6,5,10,7], "T":[7,6,4,None]}) #dataframe's printing print(data_frame)
Fill the empty space above the column axis.
# applying the ffill() method to fill the missing values Print.ffill(axis = 1)
As you can see, NaN is the first column’s value since there is no cell left to fill it with the value from the preceding cell down the column axis.
Example: Using the DataFrame.ffill() Method to fill in missing values
The missing values along the chosen axis are filled using the DataFrame.ffill() method. The example below demonstrates the same.
# begin by importing the panda's library as pd import pandas as pd #Also, import the numpy library as np import numpy as np #making a DataFrame with empty values data_frame=pd.DataFrame({"Q":[2,None,4],"R":[None,4,np.nan],"S":[2,0.25,np.nan],"T":[9,4,None]}) print("---The given DataFrame is------") print(data_frame) print("----Making up for missing values --------") print(data_frame.ffill(axis=1))
Example #1: Filling the values along the columns with the ffill() method
This example herein will show you how to use the pandas “fill()” method responsible for filling the NaN values along the column axis in a data frame. Let’s start putting this strategy to use.
The Python terminal tool is first started, and Python programming is now ready. The program’s requirement, loading the Pandas library, must first be obtained. To use “pd.DataFrame()” and “DataFrame.ffill()” pandas’ methods in this example, which can only be used as long as we have access to this library, we must import this library into a Python code.
We must use Pandas’ “pd.DataFrame()” method to create a data frame. The “D1”, “D2”, “D3”, and “D4” columns call the method and initialize it. Here, the first column, “D1,” has the following values: “3”, “15”, “9”, “6” and None. The records for “D2” are “15,” “11,” “None,” “6”, and “5.” “D3” has the entries “None”, “16”, “3”, “10”, and “9” “D4” contains the following values: “13” “5” “18” “10” and “None.” Subsequently, save this data frame in the “result_var” data frame object. Now, we have used the “print()” method to show this dataframe on the console.
importing pandas as pd import pandas as pd # dataframe's creation result_var=pd.DataFrame({"D1":[5,17,9,8,None], "D2":[17,13,None,8,7], "D3":[None,18,5,12,11], "D4":[15,7,20,12,None] }) # printing of the resultant dataframe print(result_var)
To see the generated data frame, this piece of code is run. You can see that the data frame comprises four columns and that we have a NaN value in each. We have four null items in total in the data frame.
We have used the pandas “DataFrame.ffill()” method to fill these null values along the column axis in the data frame. The “DataFrame.ffill()” function was used. Since we are populating the columns with null values for this demonstration, we used the “axis” argument and set it to “1,” which refers to the column axis. The entire script line is written as “result_var.ffill(axis=1),” and then we’ve put this function between the braces of the “print()” method and called it since we need to display the filled dataframe as a result on the console.
# applying the ffill() method to fill the missing values print(result_var.ffill(axis=1))
We then have the dataframe shown below. The first column’s value is NaN since there isn’t a column left along the column axis to fill it with the value from the preceding column.
Example: Using the DataFrame.ffill() Method to fill in missing values
When inplace=True, the DataFrame.ffill() method fills in the missing values along the designated axis and returns None. The example below demonstrates the same.
# begin by importing the panda's library as pd import pandas as pd #Also, import the numpy library as np import numpy as np #making a DataFrame with empty values data_frame=pd.DataFrame({"Q":[2,None,6],"R":[None,6,np.nan],"S":[4,2.25,np.nan],"D":[11,6,None]}) print("---The given DataFrame is------") print(data_frame) print("----Making up for missing values --------") print(data_frame.ffill(axis=1,inplace=True))
Example: Using the DataFrame.ffill() Method to fill in missing values
If the limit method is chosen, it represents the maximum number of consecutive NaN values to forward fill in the DataFrame. The example below demonstrates the same.
# import the panda's library as pd import pandas as pd #further, import the numpy library as np import numpy as np #DataFrame's creation with null values data_frame=pd.DataFrame({"Q":[4,None,6],"R":[None,6,np.nan],"S":[4,2.25,np.nan],"T":[11,6,None]}) print("---DataFrame is------") print(data_frame) print("----Filling missing values--------") print(data_frame.ffill(axis=1,limit=2))
Conclusion
Every data science strategy must include a plan for handling missing data. Missing values are frequently ignored, entries with incomplete records are dropped, and the missing values are filled in. In this lesson, we have examined the Pandas “DataFrame.ffill()” function to fill in missing data.
We explored the Python pandas DataFrame.ffill() method in this article with the help of various examples. Using this method on the DataFrame and learning the syntax and parameters, we solved examples and comprehended the DataFrame.ffill() function.