In: Computer Science
The table below contains various grades for three students obtained over the course of the year. Assuming that all grades are equally weighted, generate a new table that displays the name of the student, their average grade, and the number of grades they had in their record.
import numpy as np
np.random.seed(seed=0)
Students = ['Samir','Mark','Zoe','Andrew','Rupert']
Grades_Data = []
for i in range(100):
Student_Index = np.random.randint(0,len(Students))
Student = Students[Student_Index]
Grade = round(np.random.normal(loc=75,scale=15),1)
Grades_Data.append([Student,Grade])
import pandas as pd
Grades_df = pd.DataFrame(data=Grades_Data,columns=['Student','Grade'])
Grades_df.iloc[0:10]
#CODE TO COPY
import numpy as np
np.random.seed(seed=0)
Students = ['Samir','Mark','Zoe','Andrew','Rupert']
Grades_Data = []
for i in range(100):
Student_Index = np.random.randint(0,len(Students))
Student = Students[Student_Index]
Grade = round(np.random.normal(loc=75,scale=15),1)
Grades_Data.append([Student,Grade])
import pandas as pd
Grades_df =
pd.DataFrame(data=Grades_Data,columns=['Student','Grade'])
#To get average and count of each student using groupby
final_df = Grades_df.groupby('Student').agg(["mean","count"]).reset_index()
#To drop aggregrate columns name like mean count
final_df.columns = final_df.columns.droplevel(0)
#To set our own columns
final_df.columns = ['student_name', 'average_grade','number_of_grades']
#Final table
final_df