Home > Backend Development > Python Tutorial > How to Fill NaN Values in a Pandas DataFrame with Preceding or Following Values?

How to Fill NaN Values in a Pandas DataFrame with Preceding or Following Values?

Mary-Kate Olsen
Release: 2024-12-03 11:01:09
Original
425 people have browsed it

How to Fill NaN Values in a Pandas DataFrame with Preceding or Following Values?

Replacing NaNs with Preceding or Subsequent Values in Pandas DataFrame

Question

Consider a DataFrame with NaNs:

In [1]:
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])

In [2]:
df
    0   1   2
0   1   2   3
1   4 NaN NaN
2 NaN NaN   9
Copy after login

The goal is to replace each NaN with the closest non-NaN value in the same column above it. Assume the first row never contains NaNs.

Answer

An efficient way to achieve this is to use the fillna method of the DataFrame:

In [3]:
df.fillna(method='ffill')
    0   1   2
0   1   2   3
1   4   2   3
2   4   2   9
Copy after login

The fillna method employs the forward fill (ffill) strategy, which replaces NaNs with the last valid observation in that column.

To do the opposite, you can use the bfill method (backward fill):

In [4]:
df.fillna(method='bfill')
    0   1   2
0   1   2   3
1   4   4   3
2   9   9   9
Copy after login

The fillna method doesn't modify the DataFrame inplace. To update the original DataFrame, set inplace=True:

In [5]:
df.fillna(method='ffill', inplace=True)

In [6]:
df
    0   1   2
0   1   2   3
1   4   2   3
2   4   2   9
Copy after login

The above is the detailed content of How to Fill NaN Values in a Pandas DataFrame with Preceding or Following Values?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template