0

trying to add a boolean column to a df based on two mutually exclusive conditions:

  1. df['Type'] == 'CMBX'
  2. df['SubType'].isin(['EM','NAHY'])

they work separately

df['Px Quoted'] = np.where(df['Type'] =='CMBX', True, False)
df[df['Type']=='CMBX'].head(5)
Out[72]: 
  Batch  Type SubType  Px Quoted
0   NaN  CMBX               True
1   NaN  CMBX               True
2   NaN  CMBX               True
3   NaN  CMBX               True
4   NaN  CMBX               True

or

df['Px Quoted'] = np.where(df['SubType'].isin(['EM','NAHY']), True, False)

df[df['SubType']=='EM'].head(5)
Out[74]: 
     Batch Type SubType  Px Quoted
21  NY1530  CDX      EM       True
29  NY1530  CDX      EM       True
36  NY1530  CDX      EM       True
43  NY1530  CDX      EM       True
50  NY1530  CDX      EM       True

but the following does not

df['Px Quoted'] = np.where(df['Type'] =='CMBX' or df['SubType'].isin(['EM','NAHY']), True, False)

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

not sure why it is ambiguous as Type CMBX cannot contain any of Subtype ['EM','NAHY'] Any ideas? is that because its Subtype is blank?

gregV
  • 987
  • 9
  • 28
  • 1
    Use `|` with each condition wrapped in paranetheses, e.g. `(df[col] == 'a') | (df[col2] == 'b')`. – jpp Aug 08 '18 at 17:02

1 Answers1

1

With np.where you need to use bitwise operators:

df['Px Quoted'] = np.where((df['Type'] =='CMBX') | (df['SubType'].isin(['EM','NAHY'])), True, False)

Here I have changed the or for a |

Alex
  • 6,610
  • 3
  • 20
  • 38