Home > OS >  Fill nan value when condition meet
Fill nan value when condition meet

Time:10-15

I have a dataframe as shown below:

df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10,np.nan, np.nan, 10, 11]})

I wanna fill the nan value only when the previous non-nan value equals the flowing non-nan values.

Expected Output:

df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10, np.nan, np.nan,10, 11],'fill_nan': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 9, 10, 10, 10,10, 11]})

CodePudding user response:

Try comparing the bfill with ffill:

s = df.ffill()
out = s.where(s.eq(df.bfill()))

Output:

         A
0      1.0
1      NaN
2      NaN
3      NaN
4      3.0
5      3.0
6      3.0
7      3.0
8      NaN
9      NaN
10     NaN
11     5.0
12     5.0
13     5.0
14     6.0
15     6.0
16     6.0
17     6.0
18     6.0
19     6.0
20     6.0
21     7.0
22     8.0
23     9.0
24    10.0
25    10.0
26    10.0
27    10.0
28    11.0
  • Related