如何将列与列组合以获取数据列表中的结果(Python)?

Posted

技术标签:

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

谢谢。

【问题讨论】:

【参考方案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

【讨论】:

感谢您的帮助。

以上是关于如何将列与列组合以获取数据列表中的结果(Python)?的主要内容,如果未能解决你的问题,请参考以下文章

s-s-rS 如何将列与组中的第一列进行比较

SQL行转列与列转行(转)

如何使 ag-grid 列与列中最大的文本一样宽

熊猫:将列中的列表拆分为多行[重复]

Pandas - 将列值组合到新列中的列表中

hive函数之~行转列与列转行