根据熊猫数据框另一列的值填充空值

时间:2019-04-15 14:16:21

标签: python pandas numpy dataframe

我想根据其他列的值填充列的空值。 如果其他列为0,我想将其填充为0,否则将其保留为null。

A  B   C
1  1   0
0 NAN  2
2 NAN  0

I want the result as

A   B   C
1   1   0
0  NAN  2
2   0   0

2 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

df['B'] = np.where(df['C']== 0, 0, np.nan)

答案 1 :(得分:0)

我正在使用np.where

df['B']=np.where(df.B.isnull()&df.C.eq(0),0,df.B)
相关问题