繁体   English   中英

从CSV文件创建列平均值列表

[英]Creating a list of column averages from a csv file

我知道我之前曾问过这个问题,但是我仍然不确定为什么在尝试测试此功能时会出现此错误。 谁能帮我解决这个问题?

new_list.append(new_list [i] + num_list [i])builtins.TypeError:+不支持的操作数类型:'int'和'str'

这是文件:

Last Name,First Name,Student No.,uTORid,A1,A2,A3,A4
Smith, Joe,9911991199,smithjoe9,99,88,77,66
Ash, Wood,9912334456,ashwood,11,22,33,44
Full, Kare,9913243567,fullkare,78,58,68,88

我想获得每项作业的全班平均水平。 像99、11和78之和,然后求出平均值。 与其他作业相同。

def class_avg(open_file):
'''(file) -> list of float
Return a list of assignment averages for the entire class given the open
class file. The returned list should contain assignment averages in the 
order listed in the given file.  For example, if there are 3 assignments 
per student, the returned list should 3 floats representing the 3 averages.
class_avg -> [assignment_1_class_avg, assignment_2_class_avg...]
[62.666666666666664, 56.0, 59.333333333333336, 66.0]
'''
new_list = []
count = 0
for line in open_file:
    num_list = line.split(',')[4:]
    for i in range(len(num_list)):
        new_list.append(count)
        new_list.append(new_list[i] + num_list[i])
        count +=1
        avg = sum(float(new_list))/len(new_list)
return new_list

Patrick Artner在评论中所述,您将int和字符串加在一起,从而导致错误。 在这种情况下,使用pandas库进行CSV读取非常有用:

import pandas as pd

def class_avg(file_path):
    df = pd.read_csv(file_path)

    not_grades = 4  # Number of attributes that are not grades
    total_attributes = len(df.columns)
    avg = []

    for i in range(not_grades, total_attributes):
        avg.append(df.iloc[:, i].mean(axis=0))  # Get the column by index and calculate its mean

    return avg

这段代码正是您想要的。 但是,您需要说明在成绩之前有多少个属性,因为它们不是唯一带有数字的属性(否则,它也会计算学生的数字的平均值)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM