I have a dataframe called fresp, and in command line I can do this:
for i in range(0,len(fresp)):
num=fresp.at[i,'caseid']
And that works just fine. The exact same code, when saved in a .py file, gives me:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
specifically in reference to num=fresp.at[i,'caseid'].
Here is the whole function. When I save it in a .py file, import the function, and run it, I get the error. When I just copy the whole thing into command line (minus then first def line), it runs just fine. Why would this be?
def ValidatePregs(df,fresp):
pregmap=MakePregMap(df)
counter=0
for i in range(0,len(fresp)):
case=fresp.at[i,'caseid']
num=fresp.at[i,'pregnum']
if num == 0 and case in pregmap:
print("Mismatch for "+case+".")
counter +=1
elif num != len(pregmap[case]):
print("Mismatch for "+case+".")
counter +=1
if counter==0:
print("No mismatches!")
(I'm reading/doing Think Stats 2.0.35 by Allen B. Downey. I'm working on Exercise 1. I know there's a more elegant solution, but I still want to know why this does/doesn't work.)
ValidatePregs, and what are the first few lines of your data?