简体   繁体   English

CSV 文件列最小值 python

[英]CSV file column minimum python

Name  Gender Physics Maths
 A             45     55
 X             22     64

I have a csv file like the above.我有一个像上面这样的 csv 文件。 I want to find things like min, max etc based on subject I have figured out the list in the form [[A, 45, 55],[X, 22, 64]]我想根据主题找到诸如最小、最大等之类的东西我已经以 [[A, 45, 55],[X, 22, 64]]

How do I separate the individual columns either from this list or from the whole csv file如何将此列表或整个 csv 文件中的各个列分开

I want to do this without csv module or pandas我想在没有 csv 模块或 pandas 的情况下执行此操作

for line in filename:
    line2 = line.strip()
    line2 = line2.split(',')
    line_min = min(float(i) for i in line2)
    minimum_marks.append(line_min)
    #minimum_marks.append(minimum_grades)

    print(minimum_marks)

I am getting an error我收到一个错误

line_min = min(float(i) for i in line2)

ValueError: could not convert string to float: 'C' ValueError:无法将字符串转换为浮点数:'C'

 line_min = min(float(i) for i in line2)

ValueError: could not convert string to float: 'C' ValueError:无法将字符串转换为浮点数:'C'

line2 is a list that looks something like this: line2是一个看起来像这样的列表:

['Name', '88', '90']

Note that the first element is a name and can't be converted into an integer, so to fix your code use:请注意,第一个元素是名称,不能转换为 integer,因此要修复您的代码,请使用:

 line_min = min(float(i) for i in line2[1:])

To remove the first item of the list删除列表的第一项

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

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