如何根据另一列中的值填充一行中的空值。
A B
0 5
1 NAN
1 6
0 NAN
对于B中的空值,如果A中对应的值为0则用之前的值填充。
A B
0 5
1 NAN
1 6
0 6
```
it want it to be like this
最佳答案
numpy.where
+ isnull
+ ffill
df.assign(
B=np.where(df.A.eq(0) & df.B.isnull(), df.B.ffill(), df.B)
)
A B
0 0 5.0
1 1 NaN
2 1 6.0
3 0 6.0
关于python - 根据另一列中的值有条件地填充 pandas 中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55750556/