1

This is a mock dataframe of a much larger dataframe. I have this:

ID Category Shipped T/F? Hold T/F?
123 ABC False False
456 ABC False True
789 ABC True False
234 ABC True True
567 DEF False False
678 DEF False False
135 DEF False False
246 DEF False False
369 KGH True True

I want this:

ID Category Shipped T/F? Hold T/F? NewCol
123 ABC False False 1
456 ABC False True 1
789 ABC True False 1
234 ABC True True 0
567 DEF False False 0
678 DEF False False 0
135 DEF False False 0
246 DEF False False 0
369 KGH True True 0

My code:

import pandas as pd
import numpy as np

data=[
    ['123','ABC', False, False],
    ['456','ABC', False, True],
    ['789','ABC', True, False],
    ['234','ABC', True, True],
    ['567','DEF', False, False],
    ['678','DEF', False, True],
    ['135','DEF', True, False],
    ['246','DEF', True, True],
    ['369','KGH', True, True]
]
df = pd.DataFrame(data,columns=['ID','Category','Shipped T/F?','Hold T/F?'])

Conditions = [
    (df['Category'] != 'ABC'),
    (df['Shipped T/F?'].bool==True and df['Hold T/F?'].bool==True)
    (df['Category'] == 'ABC')
]
values=[0,0,1]
df['NewCol'] = np.select(Conditions,values)

print(df)

This is the error I'm getting: Exception has occurred: TypeError 'bool' object is not callable File "C:\Users\Johnr50\Documents\repos\SLHRoutine\testTFLkup.py", line 19, in (df['Shipped T/F?'].bool==True and df['Hold T/F?'].bool==True) TypeError: 'bool' object is not callable

I have tried the following solutions: https://www.learndatasci.com/solutions/python-valueerror-truth-value-series-ambiguous-use-empty-bool-item-any-or-all/ https://www.dataquest.io/blog/tutorial-add-column-pandas-dataframe-based-on-if-else-condition/ And several others form StackOverflow which I can no longer find since I am posting this question. Update a df column based on three other columns values using a function Create New True/False Pandas Dataframe Column based on conditions

1 Answers1

1

Modify Conditions:

Conditions = [
    df['Category'] != 'ABC',
    df['Shipped T/F?'] & df['Hold T/F?'],  # <- HERE
    df['Category'] == 'ABC'
]

Output:

>>> out
    ID Category  Shipped T/F?  Hold T/F?  NewCol
0  123      ABC         False      False       1
1  456      ABC         False       True       1
2  789      ABC          True      False       1
3  234      ABC          True       True       0
4  567      DEF         False      False       0
5  678      DEF         False       True       0
6  135      DEF          True      False       0
7  246      DEF          True       True       0
8  369      KGH          True       True       0
Corralien
  • 109,409
  • 8
  • 28
  • 52