Signup/Sign In

Pandas DataFrame ffill() Method

In this tutorial, we will learn the Python pandas DataFrame.ffill() method. This method fills the missing value in the DataFrame and the fill stands for "forward fill" and it takes the last value preceding the null value and fills it.

The below shows the syntax of the Python pandas DataFrame.ffill() method.

Syntax

DataFrame.ffill(axis=None, inplace=False, limit=None, downcast=None)

Parameters

axis:{0 or ‘index’, 1 or ‘columns’}. Axis along which to fill missing values.

inplace:bool, default False.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: int, default None. If the 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 the 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:dict, default is None. 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).

Example 1: Filling missing values using the DataFrame.ffill() Method

The DataFrame.ffill() method fills the missing values along the specified axis. The below example shows the same.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating DataFrame with null values
df=pd.DataFrame({"A":[2,None,4],"B":[None,4,np.nan],"C":[2,0.25,np.nan],"D":[9,4,None]})
print("---DataFrame is------")
print(df)
print("----Filling missing values--------")
print(df.ffill())

Once we run the program we will get the following output.


---DataFrame is------
A B C D
0 2.0 NaN 2.00 9.0
1 NaN 4.0 0.25 4.0
2 4.0 NaN NaN NaN
----Filling missing values--------
A B C D
0 2.0 NaN 2.00 9.0
1 2.0 4.0 0.25 4.0
2 4.0 4.0 0.25 4.0

Example 2: Filling missing values using the DataFrame.ffill() Method

The DataFrame.ffill() method fills the missing values along the specified axis. The below example shows the same.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating DataFrame with null values
df=pd.DataFrame({"A":[2,None,4],"B":[None,4,np.nan],"C":[2,0.25,np.nan],"D":[9,4,None]})
print("---DataFrame is------")
print(df)
print("----Filling missing values--------")
print(df.ffill(axis=1))

Once we run the program we will get the following output.


---DataFrame is------
A B C D
0 2.0 NaN 2.00 9.0
1 NaN 4.0 0.25 4.0
2 4.0 NaN NaN NaN
----Filling missing values--------
A B C D
0 2.0 2.0 2.00 9.0
1 NaN 4.0 0.25 4.0
2 4.0 4.0 4.00 4.0

Example 3: Filling missing values using the DataFrame.ffill() Method

The DataFrame.ffill() method fills the missing values along the specified axis, if inplace=True fills in-place and returns None.The below example shows the same.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating DataFrame with null values
df=pd.DataFrame({"A":[2,None,4],"B":[None,4,np.nan],"C":[2,0.25,np.nan],"D":[9,4,None]})
print("---DataFrame is------")
print(df)
print("----Filling missing values--------")
print(df.ffill(axis=1,inplace=True))

Once we run the program we will get the following output.


---DataFrame is------
A B C D
0 2.0 NaN 2.00 9.0
1 NaN 4.0 0.25 4.0
2 4.0 NaN NaN NaN
----Filling missing values--------
None

Example 4: Filling missing values using the DataFrame.ffill() Method

If the limit method is specified, this is the maximum number of consecutive NaN values to forward fill in the DataFrame. The below example shows the same.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating DataFrame with null values
df=pd.DataFrame({"A":[2,None,4],"B":[None,4,np.nan],"C":[2,0.25,np.nan],"D":[9,4,None]})
print("---DataFrame is------")
print(df)
print("----Filling missing values--------")
print(df.ffill(axis=1,limit=2))

Once we run the program we will get the following output.


---DataFrame is------
A B C D
0 2.0 NaN 2.00 9.0
1 NaN 4.0 0.25 4.0
2 4.0 NaN NaN NaN
----Filling missing values--------
A B C D
0 2.0 2.0 2.00 9.0
1 NaN 4.0 0.25 4.0
2 4.0 4.0 4.00 NaN

Conclusion

In this tutorial, we learned the Python pandas DataFrame.ffill() method. We learned the syntax, parameter and by applying this method on the DataFrame we solved examples and understood the DataFrame.ffill() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.