Pandas 前向填充,但仅在相等值之间
Pandas forward fill, but only between equal values
我有两个数据框:主数据框和辅助数据框。我正在将辅助连接到主要。它在几行中产生 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)
当前结果:
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
预期结果:
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
如果我没理解错的话,你想要的可以这样实现。您想填充 NaN,其中回填和正向填充给出相同的值。
ff = df.aux.ffill()
bf = df.aux.bfill()
df.aux = ff[ff == bf]
我有两个数据框:主数据框和辅助数据框。我正在将辅助连接到主要。它在几行中产生 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)
当前结果:
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
预期结果:
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
如果我没理解错的话,你想要的可以这样实现。您想填充 NaN,其中回填和正向填充给出相同的值。
ff = df.aux.ffill()
bf = df.aux.bfill()
df.aux = ff[ff == bf]