shape: (5, 2)
| Company | Number |
|---|---|
| str | i64 |
| "Meta" | null |
| "Facebook" | 1001 |
| "Google" | 1020 |
| "OpenAI" | 2304 |
| "Oracle" | 1022 |
backward_fill
Joram Mutenge
2025-10-04
Anyone working with data knows that encountering null values in a dataset is inevitable. There are several ways to fill null values in Polars. One approach is to replace each null value with the next non-null value that appears after it. Below is a dataframe showing companies and their corresponding company numbers.
| Company | Number |
|---|---|
| str | i64 |
| "Meta" | null |
| "Facebook" | 1001 |
| "Google" | 1020 |
| "OpenAI" | 2304 |
| "Oracle" | 1022 |
To replace null values with the next value that follows them, use the Polars expression backward_fill as shown below:
| Company | Number |
|---|---|
| str | i64 |
| "Meta" | 1001 |
| "Facebook" | 1001 |
| "Google" | 1020 |
| "OpenAI" | 2304 |
| "Oracle" | 1022 |
Now the values in Number are filled. The opposite of this method is forward_fill.
I encourage you to enroll in my Polars course.