根据其他可用变量有条件地填充缺失值

时间:2018-06-23 14:10:43

标签: python pandas

如何有条件地使用一列来填充另一列的缺失值。

DF:

A   B   C
1   158 damage
nan 789 not damage
nan 898 damage
nan 698 damage
nan 445 not damage
0       not damage

使用C列,我想填充A列的空值。

Condition, if C == damage, then A = 1 else 0.

1 个答案:

答案 0 :(得分:2)

使用numpy.where

df['A'] = np.where(df['C'] == 'damage', 1, 0)

或者,更习惯地说:

df['A'] = (df['C'] == 'damage').astype(int)