Tomasz Przemski
Tomasz Przemski

Reputation: 1127

How fill the missing values in the dataframe as ffill() function needs nan/None which are not in my dataframe?

As in the question above, is there any other way to do it? Because I care about not using values np.nan.

Input dataframe:

col_1
a
x
b
x
x
c
x

Output:

col_1
a
a
b
b
b
c
c

Upvotes: 2

Views: 65

Answers (1)

nikeros
nikeros

Reputation: 3379

Well, a quick way could be:

df["col_1"].str.replace("^x$", lambda x: np.nan).ffill()

So, instead of rebuilding a ffill logic, just try to leverage the existing one.

Upvotes: 1

Related Questions