【发布时间】:2021-06-06 20:40:02
【问题描述】:
我正在使用 Python 中的 CSV 文件。 我尝试阅读并将其拆分为一个列表:
# read file
with open("StudentsPerformance.csv") as file:
data = file.read().split("\n")
header = data[0]
students = data[1:]
# remove last student (empty student)
students.pop()
# get total number of students
total_student = len(students)
# split header
header = header.split(",")
subjects = header[5:]
# split each student in list
for i in range(len(students)):
students[i] = students[i].split(",")
for i in range(len(students)):
print(students[i])
然后,我有一个像这样的数据列表:
['female', 'group B', "bachelor's degree", 'standard', 'none', '72', '72', '74']
['female', 'group C', 'some college', 'standard', 'completed', '69', '90', '88']
['female', 'group B', "master's degree", 'standard', 'none', '90', '95', '93']
['male', 'group A', "associate's degree", 'free/reduced', 'none', '47', '57', '44']
['male', 'group C', 'some college', 'standard', 'none', '76', '78', '75']
['female', 'group B', "associate's degree", 'standard', 'none', '71', '83', '78']
['female', 'group B', 'some college', 'standard', 'completed', '88', '95', '92']
每列的名称是性别、种族、父母教育程度、备考课程、数学成绩、阅读成绩和写作成绩。
那么,我怎样才能得到每个组性别(女性、男性)的三门科目(数学、阅读、写作)的平均成绩?比如女性平均分83.33,男性63.83。
我无法使用 Pandas 库。
谢谢。
【问题讨论】: