简体   繁体   English

Python:如何在数据列中查找最高编号

[英]Python: How to find the highest number in a column of data

I have csv and txt files and I want to analyze them and find the highest number in a specific column. 我有csv和txt文件,我想分析它们并在特定列中找到最大的数字。 For example I want to know the highest number (in all the rows) in column 5. This what I have so far but I don't know how to figure out how to search a specific column. 例如,我想知道第5列中(所有行中)的最高数字。这是我到目前为止所拥有的,但是我不知道如何弄清楚如何搜索特定的列。

`import csv

#opening csv
file = open("Scoring.csv","r")
csv = csv.reader(file) 
csv_1=[]

rows = []
for in_line in file:
    row = [float(each) for each in in_line.split()]
    rows.append(row)

file.close() # this'll happen at the end of the script / function / method anyhow

columns = zip(*rows)

for index, row in enumerate(rows):
    print "In row %s, Max = %s, Min = %s" % (index, max(row), min(row))

for index, column in enumerate(columns):
    print "In column %s, Max = %s, Min = %s" % (index, max(column), min(column))

If the following code 如果以下代码

for index, column in enumerate(columns):
    print "In column %s, Max = %s, Min = %s" % (index, max(column), min(column))

you provided gives you correct answers and I understand you correctly, than getting min value (for example) from specific column should be easy enough, along the lines of min(columns[index_of_column]), for example: 您提供的答案可以给您正确的答案,并且我理解的也很正确,例如,沿着min(columns [index_of_column])的行从特定列中获取最小值(例如)应该足够容易,例如:

min(columns[4])

For max value in column, replace min() with max(). 对于列中的最大值,将min()替换为max()。 And, remember, index_of_column is zero based (first column has index of 0, so 5th column would have an index of 4). 并且,请记住,index_of_column基于零(第一列的索引为0,因此第五列的索引为4)。

So, to get both min and max value, you would do: 因此,要获得最小值和最大值,您可以执行以下操作:

print "In column %s, Max = %s, Min = %s" % (5, max(columns[4]), min(columns[4]))

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

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