-1

In Python, I'm trying to create code that iterates and checks if Gender is "E", and if yes, then if "IncState" is "ca", "california" (regardless of capitalization) or None, then it inserts "California", else it inserts the value in DefendantsTbl['IncState'][i].

I keep getting a "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." error.

This is sample data.

Name Gender ShortName Business State ZipCode IncState MergeName
0 Company1 E CWI None None None None CWI
1 Company2 E None None None None Delaware Company2

This is the code I am using.

import os, sys
import pandas as pd 
from janitor import xlsx_table
from docx.shared import Inches, Pt
import numpy as np
import xlwings as xw
from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from time import perf_counter    
doc = Document()
docap = doc.add_paragraph

for i in range(len(DefendantsTbl)):
if DefendantsTbl['Gender'][i].lower() == 'e':
    docap('At all times material hereto, ' + DefendantsTbl['Name'][i] + ' was a ' 
+ ('California' if (str(DefendantsTbl['IncState'][i]).lower() == 'california') 
or (str(DefendantsTbl['IncState'][i]).lower() ==
'ca') or (DefendantsTbl['IncState'][i] is None)
else DefendantsTbl['IncState'][i]) + 'corporation that regularly transacted business in ' + CaseInfoTbl['County'] + ' County, California.')
apaderno
  • 28,547
  • 16
  • 75
  • 90
Rycliff
  • 127
  • 1
  • 9

1 Answers1

1

The exception is thrown when you want to get the bool of a pandas.Series:

>>> import pandas as pd
>>> x = pd.Series([1])
>>> bool(x)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What you hit was a place where the operator implicitly converted the operands to bool (you used if but it also happens for and, or and while):

>>> df or df
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
>>> df and df
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
>>> if df:
...     print('fun')
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
>>> while df:
...     print('fun')
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Pythoneer
  • 319
  • 1
  • 16