In: Computer Science
Why am I getting the error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
Fielding is a DataFrame, but I am not sure why I am getting this error. Any help would be appreciated!
RAR = [] for i in range(0,len(Fielding)): position = (Fielding['POS'][i]) value = 0 if position == 'C': value = (9.0/150.0) * (Fielding['GS'][i]) elif position == 'SS': value = (7.0/150.0) * (Fielding['GS'][i]) elif position == '2B': value = (3.0/150.0) * (Fielding['GS'][i]) elif position == '3B': value = (2.0/150.0) * (Fielding['GS'][i]) elif position == '1B': value = (-9.5/150.0) * (Fielding['GS'][i]) else: value = 0 RAR.append(value)
Hi,
Above code don't have any error here if Data frame is like this(sample):
data = {'POS':['C', 'SS', '2B', '1B'],
'GS':[150, 300, 450, 600]}
# Create DataFrame
Fielding = pd.DataFrame(data)
check program and output here:
Output:
You mentioned you are getting: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() error.
Basically this error occurs when you tried to compare array of values. Let's check this example:
Output:
If you use .any method it check any one of the element in array is true, then it return true.
In comparing with integers true treated as 1.
Check modified version of program:
Output:
From the above code
df['heart rate'].any() return True, but 50 < df['heart rate'].any() < 101 returns false true treated as 1 so, 50 < 1<101 will become false. Similarly for all .any() method also. Finally output is false.
So basically you may getting error from other piece of your code. Please share other code also(In comment) if you need more information.