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?
CodePudding user response:
Try:
df["new"] = np.where(df["code"].isin([0,1]), 1, 0)
CodePudding user response:
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
CodePudding user response:
df['new'] = df['code'].isin([0,1])*1
CodePudding user response:
df['new']= np.where((df['code']== 0) | (df['code']== 1), 1 , 0)