How to combine column with column to get result in data list (Python)?

I’m working with the CSV file in Python. I tried to read and split it into a list:

# read file
with open("StudentsPerformance.csv") as file:
    data ="n")

header = data[0]
students = data[1:]

# remove last student (empty student)

# 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)):

Then, I have a data list like:

['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']

The name of each column is gender, race, parental level of education, test preparation course, math score, reading score, and writing score.

So, how can I get the average grade of three subjects (math, reading, writing) for each group gender (female, male)? Like the average score of Female is 83.33 and the Male is 63.83.

I can not use Pandas libary.

Thank you.


Following code will give you average for males and females:

    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
            femaleScore += float(student[5])
            femaleScore += float(student[6])
            femaleScore += float(student[7])
            noOfFemales += 1
    maleAverageScore = maleScore/(noOfMales*3)
    femaleAverrageScore = femaleScore /(noOfFemales*3)

Leave a Reply

Your email address will not be published. Required fields are marked *