Suppose I have a DataFrame with some NaNs:
result
0 1
1 NaN
2 NaN
3 1
4 NaN
5 2
6 2
7 NaN
8 1
What I need to do is replace every NaN with the last valid value, and i expecting result like this:
result Expected_Result
0 1 1
1 NaN 1
2 NaN 1
3 1 1
4 NaN 1
5 2 2
6 2 2
7 NaN 2
8 1 1
are its possible using np.whare to achieve this replacement value?
Use, pd.Dataframe.ffill
means "forward-fill":
df.ffill()
Output:
result
0 1.0
1 1.0
2 1.0
3 1.0
4 1.0
5 2.0
6 2.0
7 2.0
8 1.0
When needed you can also use this as a function of a GroupBy objects to only forward-fill within a group. Documentation