I want to change all the number in my array into ABCD scoring system. I have 100 X 1 array of scores, my array is put under total_score. I tried using
def grade(score):
if 91 <= score <= 100:
return 'A'
if 81 <= score <= 90.99:
return 'B'
if 71 <= score <= 80.99:
return'C'
if 61 <= score <= 70.99:
return'D'
else:
return'E'
grade = float(total_score)
I also tried using
def determine_grade(scores, breakpoints=[50, 60, 70, 80, 90], grades='FEDCBA'):
i = bisect.bisect(breakpoints, scores)
return grades[i]
[grade(score) for score in [total_score]]
both doesnt work. There are also few suggestion, but it seems none of the suggestion work with pandas array
CodePudding user response:
Let's say your total scores array looks like this:
total_scores = ['99', '100', '52', '69', '33', '77']
Your grading function:
def grade(score):
if 91 <= score <= 100:
return 'A'
if 81 <= score <= 90.99:
return 'B'
if 71 <= score <= 80.99:
return'C'
if 61 <= score <= 70.99:
return'D'
else:
return'E'
Then all you have to do is:
grading_score = [grade(float(score)) for score in total_scores] # returns ['A', 'A', 'E', 'D', 'E', 'C']
Note that I'm converting the strings in my array into floats before calling the 'grade' function.
Here is the official documentation for using list comprehensions