2

I have a dataframe with two boolean columns.

I am trying to assign a new column using the following logic:

df['C'] = df['A']==True and df['B']==False

The error says the truth of a Series is ambiguous. I can see the problem but I don't know how to tell it that I want it perform the operation row-wise.

jsstuball
  • 4,104
  • 7
  • 33
  • 63

2 Answers2

0
df['C']=((df['A']==True) & (df['B']==False))*1
Yury Wallet
  • 1,474
  • 1
  • 13
  • 24
0

Use:

df['C'] = (df['A']==True) & (df['B']==False) 

Or better as pointed @Yakym Pirozhenko in comment is invert B column by ~:

df['C'] = df['A'] & ~df['B']

Sample:

df = pd.DataFrame({'A':[True, False, True, True],
                   'B':[True, False, False, True]})

print (df)
       A      B
0   True   True
1  False  False
2   True  False
3   True   True

df['C'] = df['A'] & ~df['B']
print (df)
       A      B      C
0   True   True  False
1  False  False  False
2   True  False   True
3   True   True  False
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252