create new column with mask pandas 1

create new column with mask pandas

mask0 = (df['condition'] == 1)
mask1 = (df['condition'] == 0)

df.loc[mask0, 'newColumn'] = df.loc[mask0, 'otherColumn1']
df.loc[mask1, 'newColumn'] = df.loc[mask1, 'otherColumn2']

Here is what the above code is Doing:
1. Create a mask for condition == 1
2. Create a mask for condition == 0
3. For rows where condition == 1, set newColumn to otherColumn1
4. For rows where condition == 0, set newColumn to otherColumn2

Similar Posts