-2

I want to add a new column to my Data frame in the following manner:

df['new']= np.where(df['code']== 0 or 1, 1, 0 )

Here, I want to assign the value 1 to the 'new' column when the values in the code column is either 0 or 1. It gives an error. But if I'm using only one condition, the statement works.

The following statement works:

df['new']= np.where(df['code']== 0, 1, 0 )

How do I use both conditions while assigning values to the new column?

Ayush Sinha
  • 41
  • 1
  • 7
  • See [this](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-single-value) for why your condition doesn't work. – not_speshal Oct 26 '21 at 14:05

4 Answers4

1

Try:

df["new"] = np.where(df["code"].isin([0,1]), 1, 0)
not_speshal
  • 22,093
  • 2
  • 15
  • 30
0
df['new']= np.where((df['code']== 0) | (df['code']== 1), 1 , 0)
0

You don't have to use np.where, use a boolean mask and convert it to int:

df['new'] = df['code'].isin([0, 1]).astype(int)

Example:

>>> df
  code
0    2
1    3
2    0
3    3
4    1

>>> df['new'] = df['code'].isin([0, 1]).astype(int)

>>> df
  code  new
0    2    0
1    3    0
2    0    1
3    3    0
4    1    1
Corralien
  • 109,409
  • 8
  • 28
  • 52
0
df['new'] = df['code'].isin([0,1])*1
MaPy
  • 505
  • 1
  • 6
  • 9