1

I want to create a new column in my pandas dataframe based on values in already existing columns. The input of the new column should be boolean. At the moment I am trying the following:

import pandas as pd df_edit = pd.DataFrame({'Included': [False, False, True, False], 'Update Check': [True, True, True, True], 'duplicate_fname': [True, False, False, False], 'duplicate_targetfname': [False, False, False, False]}) df_edit['test'] = df_edit['Included'] == False & df_edit['Update Check'] == True & (df_edit['duplicate_fname'] == True | df_edit['duplicate_targetfname'] == True) 

When I try to do it like this I get a ValueError stating the following:

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

Is there another way to do this?

My expected output would be a column that consists of the following values:

True, False, False, False 
1
  • 1
    They have to be enclosed in braces. Commented May 30, 2018 at 8:05

1 Answer 1

1

There is problem not enclosing parentheses:

df_edit['test'] = (df_edit['Included'] == False) & \ (df_edit['Update Check'] == True) & \ ((df_edit['duplicate_fname'] == True) | (df_edit['duplicate_targetfname'] == True)) print (df_edit) Included Update Check duplicate_fname duplicate_targetfname test 0 False True True False True 1 False True False False False 2 True True False False False 3 False True False False False 

But better is use ~ for invert boolean mask and omit compare with Trues:

df_edit['test'] = ~df_edit['Included'] & df_edit['Update Check'] & (df_edit['duplicate_fname'] | df_edit['duplicate_targetfname']) print (df_edit) Included Update Check duplicate_fname duplicate_targetfname test 0 False True True False True 1 False True False False False 2 True True False False False 3 False True False False False 
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that was fast. Thank you very much!
@LaurensdeWit - I obviously remove answer because dupe, but becauxe here is better solution keep it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.