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

Viewed 34

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
1 Answers

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.

Related