【问题标题】:How to combine column with column to get result in data list (Python)?如何将列与列组合以获取数据列表中的结果(Python)?
【发布时间】: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 库。

谢谢。

【问题讨论】:

    标签: python list csv


    【解决方案1】:

    以下代码将为您提供男性和女性的平均值:

        maleScore = 0
        femaleScore = 0
        noOfMales = 0
        noOfFemales = 0
        
        for student in students:
            if student[0] == "male":
                maleScore += float(student[5])
                maleScore += float(student[6])
                maleScore += float(student[7])
                noOfMales += 1
            else:
                femaleScore += float(student[5])
                femaleScore += float(student[6])
                femaleScore += float(student[7])
                noOfFemales += 1
        
        maleAverageScore = maleScore/(noOfMales*3)
        femaleAverrageScore = femaleScore /(noOfFemales*3)
    

    【讨论】:

    • 感谢您的帮助。
    【解决方案2】:

    这是一个非常简单的方法。只需使用 for 循环,将所有分数的总和相加。然后将总和除以学生人数!如果此答案有帮助,请将其设置为解决方案!

    mathScore = 0
    readingScore = 0
    writingScore = 0
    
    for student in students:
        mathScore += float(student[5])
        readingScore += float(student[6])
        writingScore += float(student[7])
    mathScore = mathScore/len(students)
    readingScore = readingScore /len(students)
    writingScore = writingScore /len(students) 
    

    【讨论】:

    • 感谢您的帮助。
    【解决方案3】:

    这是我的建议:

    def compute_average_score(student_group):
        return sum([score for student in student_group for score in list(map(int, student[-3:]))]) / (len(student_group) * 3)
    
    female_group = [x for x in students if x[0] == 'female']
    male_group = [x for x in students if x[0] == 'male']
    
    print(round(compute_average_score(female_group), 2))
    print(round(compute_average_score(male_group), 2))
    

    输出:

    83.33
    62.83
    

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2013-05-06
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多