Shash
Shash

Reputation: 47

Truth value of a Series is ambiguous. Use a.empty(), a.bool(), a.item(), a.any() or a,all()

I'm trying to write a code that takes certain (optional) inputs from a .csv file and outputs based on which criteria (inputs) have been given and which have not. I managed to get the code to take optional inputs but I want the program to take certain outputs from certain rows of the file (for example, variable w is the name of the business, I want it to read only that specified row from the .csv and ignore every other, same for every other criteria) so I tried something, here's the code:

#link CSV
    import pandas as pd

    df = pd.read_csv(r'C:\Users\ASUS\Documents\Small businesses.csv',encoding='latin1')

    print(df.to_string()) 
   #Take input
w=str(input('enter name here'))
x=str(input('enter field here'))
y=str(input('enter desired operation size here'))
z=str(input('enter preferred mode of service here'))
#refine output
#to revert what you did if it doesn't work, remove the df statements and convert
#every second if back to elif 
if df.iloc[1,4]:
 if x == "":
    print (y + z)
or df.iloc[1,7]:
 if y == "":
    print (x + z)
or df.iloc[1,11]:
 if z == "":
    print (x + y)
or df.iloc[[1,4],[1,7]]:
 if x == "" and y == "":
    print(z)
or df.iloc[[1,4],[1,11]]:
 if x == "" and z == "":
    print(y)
or df.iloc[[1,7],[1,11]]:
    if y == "" and z == "":
     print(x)
or df.iloc[[1,1]]:
 if w == "":
    print ("accoridng to other guidelines, we find:")

I know the code is wrong but I just don't know how to fix it since I'm new to programming, I got this error: "Truth value of a Series is ambiguous. Use a.empty(), a.bool(), a.item(), a.any() or a,all()" Any fixes? Thank you

edit: For all the people asking me to paste a portion of the .csv to help, here it is: small business.csv screenshot

Here's the full error from the kernel: File "C:\Users\ASUS\untitled2.py", line 29, in if df.iloc[[1,4],[1,7]]:

File "C:\Users\ASUS\AppData\Local\Programs\Spyder\pkgs\pandas\core\generic.py", line 1330, in nonzero f"The truth value of a {type(self).name} is ambiguous. "

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). (sorry I'm new to this)

Upvotes: 1

Views: 224

Answers (1)

Gulzar
Gulzar

Reputation: 28074

df.iloc[[1,4],[1,11]]

and other such statements return a pd.series, not a Boolean.

In order to evaluate those in an If statement, you have to reduce these multiple values into a single Boolean value using some logic.

Usually, you will want to use .any() or .all() for that.

Upvotes: 0

Related Questions