Pandas – DataFrame.ffill() method (5 examples)

Updated: February 24, 2024 By: Guest Contributor Post a comment


Pandas is a powerhouse tool for data analysis in Python, and its handling of missing data is one of its great strengths. One versatile method for managing missing values is the .ffill() method, which stands for ‘forward fill’. This tutorial offers a deep dive into using this method across five examples, ranging from simple applications to more nuanced usages that can enhance your data preprocessing workflows.

What is the .ffill() Method Used for?

The ffill() method in Pandas is used to propagate the last known non-null value forward until another non-null value is encountered. This is particularly useful in time series data where the assumption that the next timestamp will have the same value as the last known is reasonable. However, it can be applied to any DataFrame where this assumption holds.

Basic Example

Let’s start with a basic example. We’ll create a DataFrame with some missing values and use ffill() to fill them.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'A': [1, np.nan, 3, 4, np.nan],
    'B': [np.nan, 2, np.nan, 3, 4]

print('Original DataFrame:')

# Applying ffill()
df_filled = df.ffill()
print('\nDataFrame after ffill:')

The output demonstrates how ffill() replaces NaN values with the previous non-null value in each column:

Original DataFrame:
   A    B
0  1.0  NaN
1  NaN  2.0
2  3.0  NaN
3  4.0  3.0
4  NaN  4.0

DataFrame after ffill:
   A     B
0  1.0  NaN
1  1.0  2.0
2  3.0  2.0
3  4.0  3.0
4  4.0  4.0

Axis Parameter

Next, let’s explore the axis parameter of ffill(). By default, ffill() operates vertically down columns (axis=0), but it can also fill values horizontally across rows (axis=1).

df_horizontal = df.ffill(axis=1)
print('DataFrame after horizontal ffill:')

This changes how missing values are filled, as demonstrated below:

DataFrame after horizontal ffill:
     A     B
0  1.0   1.0
1  NaN   2.0
2  3.0   3.0
3  4.0   3.0
4  NaN   4.0

Limiting the Fill

Another useful parameter of ffill() is limit, which controls the number of consecutive NaN values to fill. This can be especially helpful when you want to avoid forward filling too far, potentially introducing bias into your data.

df_limited = df.ffill(limit=1)
print('DataFrame with limited ffill:')

Here, only the first instance of a missing value after a non-null value is filled:

DataFrame with limited ffill:
    A    B
0  1.0  NaN
1  1.0  2.0
2  3.0  2.0
3  4.0  3.0
4  NaN  4.0

Applying ffill() After Grouping

In more complex datasets, you may want to apply ffill() within groups of data. This can be achieved by using .groupby() alongside ffill(). Let’s illustrate this with an example where we group by one column and apply ffill() within each group.

df['Group'] = ['X', 'X', 'Y', 'Y', 'Y']
df_grouped = df.groupby('Group').ffill()
print('DataFrame after grouped ffill:')

The output validates the assumption that filling forward makes sense within each subset of data:

DataFrame after grouped ffill:
     A    B Group
0  1.0  NaN    X
1  1.0  2.0    X
2  3.0  NaN    Y
3  3.0  3.0    Y
4  3.0  4.0    Y

Using ffill() with Time Series Data

Finally, an area where ffill() shines is in handling time series data. Using a datetime index, we can more accurately forward fill based on time intervals. Here, we’ll simulate time series data and demonstrate the application of ffill().

time_index = pd.date_range('2020-01-01', periods=5, freq='D')
df_time = pd.DataFrame({
    'Value': [1, np.nan, np.nan, 4, 5]},

print('Time series DataFrame:')

# Applying ffill()
df_time_filled = df_time.ffill()
print('\nTime series DataFrame after ffill:')

This method ensures that missing values are filled in a manner that respects the time sequence, demonstrating its power:

Time series DataFrame:
2020-01-01    1.0
2020-01-02    NaN
2020-01-03    NaN
2020-01-04    4.0
2020-01-05    5.0

Time series DataFrame after ffill:
2020-01-01    1.0
2020-01-02    1.0
2020-01-03    1.0
2020-01-04    4.0
2020-01-05    5.0


The ffill() method is an essential tool in the Pandas library for handling missing data, especially in time series analysis. Through the examples illustrated, we’ve seen that it is not only useful for filling missing values but also versatile enough to be tailored to specific data structures or needs. Of course, the context of your data and the assumptions you make about it should always guide how you choose to fill missing values.