-1

I want to create conditions as below to evaluate the HUB Status as ...

  1. CUR_RULE=DEFAULT and PREV_RULE=DEFAULT, then ok.
  2. CUR_RULE=DEFAULT and PREV_RULE!=DEFAULT, then not ok.
  3. CUR_RULE!=DEFAULT and PREV_RULE=DEFAULT, then ok.
  4. CUR_RULE!=DEFAULT and PREV_RULE!=DEFAULT, then ok.

I am trying to run the code below:

df3=df1[df1['STATUS']=='CHANGED']
df3.insert(7, 'HUB STATUS','')
df3.insert(8, 'COMMENT','')
if df3[df3['CUR_RULE']=='DEFAULT'] and df3[df3['PREV_RULE']=='DEFAULT']:
    df3['HUB STATUS']='OK'
elif df3[df3['CUR_RULE']=='DEFAULT'] and df3[df3['PREV_RULE']!='DEFAULT']:
    df3['HUB STATUS']='NOT OK'
elif df3[df3['CUR_RULE']!='DEFAULT'] and df3[df3['PREV_RULE']=='DEFAULT']:
    df3['HUB STATUS']='OK'
else:
    df3['HUB STATUS']='NOT OK'

I am getting this error:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • 1
    add pandas tag, if you actually areusing pandas module – Superior Apr 06 '21 at 19:49
  • 1
    To properly respond to your request, please provide a sample of your input dataframe. The immediate cause of your error seems to be in the following line of code and the other places where you use similar structure: ```df3[df3['CUR_RULE']=='DEFAULT'] ``` The issue is that the factor to the left of the == is a panda series, not a single value, yet you are trying to compare to a single value. – itprorh66 Apr 06 '21 at 19:59
  • 1
    Does this answer your question? [Logical operators for boolean indexing in Pandas](https://stackoverflow.com/questions/21415661/logical-operators-for-boolean-indexing-in-pandas) – Tyberius Apr 06 '21 at 20:02

1 Answers1

0

Issue: no truth values inside index

In vanilla Python logical boolean expressions can use or and and, which require truth-values on each side. Along with comparisons like == or != these form valid expressions to be used inside if-statements.

But when used inside panda's dataframe indexes (square brackets to select one column) they are invalid. Like the index attempt df3[df3['CUR_RULE']=='DEFAULT'] which contains a boolean condition inside square brackets df3['CUR_RULE']=='DEFAULT'.

For pandas these index expressions evaluates to a truth value which is considered ambiguous.

Filter: using bitwise operators inside conditions

Instead you could use bitwise operations:

  • | (or)
  • & (and)

See Difference between 'and' and '&' in Python

For example when applying filters with panda's where, like:

# filtering data
df3.where(df3['CUR_RULE'] == 'DEFAULT' & df3['PREV_RULE'] == 'DEFAULT', inplace=True)
print(df3) # show series filtered for df3['HUB STATUS']='OK'

See Pandas where: How to Use Pandas DataFrame where()

Map: using a ternary (similar to if-else)

or better using the map function, like here using a lambda:

# mapping data using a function, dict, or lambda
map_hub = lambda x: 'NOT OK' if x['CUR_RULE'] == 'DEFAULT' and x['PREV_RULE'] != 'DEFAULT' else 'OK' 

df3['HUB STATUS'] = df3.map(map_hub)

See Adding a new pandas column with mapped value from a dictionary

See also

hc_dev
  • 8,389
  • 1
  • 26
  • 38