Pandas Filling Missing Values Down Based On Row Above
I have a dataframe like the following: import pandas as pd data={'col1':[1,3,3,1,2,3,2,2, 1], 'col2':[np.nan, 1, np.nan, 1, np.nan, np.nan, np.nan, 2, np.nan]} df=pd.DataFrame(data
Solution 1:
You can use np.where
by looking at where the forward-fill is equal to one, filling 1 where it's True, and falling back to the value of 'col2' when it's False:
df['col2'] = np.where(df['col2'].ffill() == 1, 1, df['col2'])
The resulting output:
col1 col2
0 1 NaN
1 3 1.0
2 3 1.0
3 1 1.0
4 2 1.0
5 3 1.0
6 2 1.0
7 2 2.0
8 1 NaN
Solution 2:
You can use the df.fillna function with forward padding like this:
df.fillna(method='pad')
col1 col2
0 1 NaN
1 3 1.0
2 3 1.0
3 1 1.0
4 2 1.0
5 3 1.0
6 2 1.0
7 2 2.0
8 1 2.0
Solution 3:
ffilled = df.col2.ffill()
df.assign(col3=df.col2.fillna(ffilled[ffilled == 1]))
Post a Comment for "Pandas Filling Missing Values Down Based On Row Above"