pylearner
pylearner

Reputation: 1460

conditionally filling the missing values based on the other variables available

How to conditionally use a column to fill in the missing values of the other column.

DF :

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

Using column C, I want fill column A null values.

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

Upvotes: 2

Views: 86

Answers (1)

jpp
jpp

Reputation: 164683

Using numpy.where:

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

Or, more idiomatically:

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

Upvotes: 2

Related Questions