如何有条件地使用一列来填充另一列的缺失值。
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.
答案 0 :(得分:2)
使用numpy.where
:
df['A'] = np.where(df['C'] == 'damage', 1, 0)
或者,更习惯地说:
df['A'] = (df['C'] == 'damage').astype(int)