Python categorize error: This is my code: df5[\"gpa_level\"]= \"Unknown\" for i
ID: 3918343 • Letter: P
Question
Python categorize error:
This is my code:
df5["gpa_level"]= "Unknown"
for i in range(len(df5.gpa)):
gpal= [df5.ix[i,"gpa"]]
if gpal>3.80:
df5.ix[i,"gpa_level"]="A"
elif 3.50 < gpal <= 3.80:
df5.ix[i,"gpa_level"]="B"
elif 3.20 < gpal <= 3.50:
df5.ix[i,"gpa_level"]="C"
elif 2.80 < gpal <= 3.20:
df5.ix[i,"gpa_level"]="D"
elif 2.00 < gpal <= 2.80:
df5.ix[i,"gpa_level"]="E"
else: df5.ix[i,"gpa"]="F"
print(df5)
And it return: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
df5 is the dataframe in picture.
Explanation / Answer
The or and and python statements require truth-values. For pandas these are considered ambiguous so you should use "bitwise" | (or) or & (and) operations:
These are overloaded for these kind of datastructures to yield the element-wise or (or and).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.