Pandas 前向填充,但仅限于相等值之间

Mai*_*and 6 python pandas fillna

我有两个数据框:主数据框和辅助数据框。我正在将辅助连接到主连接。它导致几行中出现 NaN,我想填充它们,而不是全部。代码:

df1 = pd.DataFrame({'Main':[00,10,20,30,40,50,60,70,80]})
df1 = 
   Main
0     0
1    10
2    20
3    30
4    40
5    50
6    60
7    70
8    80
df2 = pd.DataFrame({'aux':['aa','aa','bb','bb']},index=[0,2,5,7])
df2 = 
  aux
0   aa  
2   aa
5   bb
7   bb
df = pd.concat([df1,df2],axis=1)
# After concating, in the aux column, I want to fill the NaN rows in between 
# the rows with same value. Example, fill rows between 0 and 2 with 'aa', 2 and 5 NaN, 5 and 7 with 'bb'
df = pd.concat([df1,df2],axis=1).fillna(method='ffill')
print(df)
Run Code Online (Sandbox Code Playgroud)

目前结果:

  Main aux
0    0   aa
1   10   aa
2   20   aa
3   30   aa # Wrong, here it should be NaN
4   40   aa # Wrong, here it should be NaN
5   50   bb 
6   60   bb
7   70   bb
8   80   bb # Wrong, here it should be NaN
Run Code Online (Sandbox Code Playgroud)

预期结果:

  Main aux
0    0   aa
1   10   aa
2   20   aa
3   30  NaN
4   40  NaN
5   50   bb
6   60   bb
7   70   bb
8   80  NaN
Run Code Online (Sandbox Code Playgroud)

Håk*_*Lid 9

如果我理解正确的话,你想要的可以这样完成。您想要填充 NaN,其中回填和前向填充给出相同的值。

ff = df.aux.ffill()
bf = df.aux.bfill()
df.aux = ff[ff == bf]
Run Code Online (Sandbox Code Playgroud)