Home > Net >  Fill a third column when condition applied on two columns values satisfy simultaneously
Fill a third column when condition applied on two columns values satisfy simultaneously

Time:06-07

I have a column X with 0 and 1 values and column Y with integer values. Column Z takes a value "YES" only if column X value is 1 and column Y value is >= 30. The code below takes the conditions as an "OR" statement not an "AND" statement.

conditions = [(df['X'] == 1),(df['Y'] >= 30)]
choices = ["YES", "NO"]
df['Z'] = np.select(conditions, choices, default="YES")

Dataframe, df

X   Y   Z
1   20  NO
0   10  NO
1   12  NO
0   17  NO
1   90  YES
1   70  YES
1   45  YES
0   2   NO
0   6   NO
1   8   NO

CodePudding user response:

Use

>>> np.where((df['X'] == 1) & (df['Y'] >= 30),'YES', 'NO')
  • Related