Skip to content Skip to sidebar Skip to footer

Defining Pandas Column Based On Combination Of Input Other Columns

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 follo

Solution 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  UpdateCheck  duplicate_fname  duplicate_targetfname   test
0FalseTrueTrueFalseTrue1FalseTrueFalseFalseFalse2TrueTrueFalseFalseFalse3FalseTrueFalseFalseFalse

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  UpdateCheck  duplicate_fname  duplicate_targetfname   test
0FalseTrueTrueFalseTrue1FalseTrueFalseFalseFalse2TrueTrueFalseFalseFalse3FalseTrueFalseFalseFalse

Post a Comment for "Defining Pandas Column Based On Combination Of Input Other Columns"