Calculating Grades With Pandas DataFrames
00:00 To compute the final grade for each student, we have three assessment types. In the course, there are the three exams and we’re simply going to compute a simple percentage, or average, for each of the scores of the exams, and we’ll denote these by E1, E2, and E3.
00:19 Then, there’s the homework assignments. There’s a couple of ways that we can compute the average for the homework and we’ll do both ways, and then simply compute the maximum of the two averages and give that maximum for each student, so this is going to be on a per student basis.
00:36 Then we’ll have the quizzes, and we’ll pretty much do the exact same thing for the quizzes. We’re going to compute two averages and then find the maximum of the two and give the maximum to each student. For those five different values—the three exams, the homework score, and the quiz score—we’re going to assign a weight, or a percent, of the final grade that each of those individual values will represent to compute the final grade. Those weights are going to sum up to 1.
01:06 Then, to compute the final grade for each student, we’ll just take each of the weights and then multiply those by the corresponding values and then simply sum them up.
01:18 For the weights, we’re going to define a pandas Series, and we’ll have each of those five different values. We’ll create a label, or an index, that we’ll also use in the DataFrame that we’ve constructed, and so in the big DataFrame we’re going to create five new columns with these exact same headings.
01:39 And so when we go ahead and compute the final score and perform that multiplication and summation, we’ll simply multiply the columns in the DataFrame, having these exact same headings with this Series of the weights.
01:55 Notice here that the sum of the weights is 1, and so we’re giving 5% of the final grade as going to be coming from the Exam 1, 10% from Exam 2, 15% from Exam 3, and the quiz score’s worth 30% of the final grade and the homework score is worth 40%.
02:14 Let’s start by computing the average score of the exams and adding new columns to the DataFrame that we’re constructing.
Back here in our main DataFrame that we’re constructing, we have the exam scores in the columns that are named
"Exam" and then the number and then
"Exam", number, and then maximum number of points.
02:36 All we have to do is simply just divide the exam number divided by the maximum number of points to just compute a simple average, or a simple percentage. Let’s go back over here, maybe add a little bit of a note to ourselves that we’re going to be computing now the final grades. Why don’t we write over here,
### Calculating Final Grade, and we’ll start with, say,
## Exam Grades, like we said.
We’re going to want to create a new column in the DataFrame that we’re building. We’ll start with one exam. We’ll say
'Exam 1 Score', we want that new column to simply equal the
'Exam 1' score and divide that by the total number, or the maximum number, of points that a student could have scored in that particular exam.
03:40 This will create a new column. And we want to do this for the three different exams, so maybe what we’ll do is let’s introduce a new variable, right? So instead of hard-coding this for the three different exams, let’s introduce a new variable, which will be the number of exams.
Then we’ll simply just use a
for loop, and we want to go from exam number 1, all the way to
n_exams plus 1, so that we go from
And then just use a f-string, so we’ll say
n, and then we’ll do the same thing for the other one. So here we’ll have
n and an f-string. All right, so I’ll let you put that in.
All right, so I think that looks good. Let’s run that and let’s now take a look at the
final_df. Let’s just take a look at the first five. Now at the very end, we’ve got three new columns that contain just simple percentages of the exam scores for each individual student.
04:59 All right, now let’s take a look at how we can compute a final score for the homework assignments.
Become a Member to join the conversation.