简体   繁体   English

如何在python中以数字方式对csv文件中的数据进行排序

[英]how do i sort data from a csv file numerically in python

I am writing a program that takes students scores from a csv file and needs to sort then highest to lowest score. 我正在编写一个程序,该程序从csv文件中获取学生分数,然后需要按照从高到低的顺序进行排序。 the csv file looks like this: csv文件如下所示:

josh 12 乔希12
john 6 约翰6
fred 8 fred 8
harry 7 哈利7

i have tried to put the items in a list like this: 我试图将项目放在这样的列表中:

 Mylist=[]
csvfile = open (classname,'r')
reader = csv.reader(csvfile)
for row in reader:
    Mylist.append(row)

then reverse the list to put the numerical value first: 然后反转列表以将数值放在第一位:

Mynewlist = []
    for each in Mylist:
        value2 = ''.join(each[0])
        value1 = ''.join(each[1])
        mynewlist.append(value1,value2)

with no luck i get this error: 运气不好我得到这个错误:

    Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    main()
  File "\\SRV-FILE3\ca231$\task 3\3.py", line 143, in main
    value1 = ''.join(each[1])
IndexError: list index out of range

i use ''.join(each[1]) to convert to a string and then append them in the opposite order then i planned to use .sort() to sort them numerically but I cant get them to append to a list. 我使用''.join(each[1])转换为字符串,然后以相反的顺序附加它们,然后计划使用.sort()对它们进行数字排序,但我无法将它们附加到列表中。

does anyone know how to sort the contents of a csv file by its numerical value? 有谁知道如何按其数值对csv文件的内容进行排序?

I think you're overcomplicating things. 我认为您太过复杂了。 Assuming you have the data as a list of lists: 假设您将数据作为列表列表:

data = [("josh", "12"), ("john", "6"), ("fred", "8"), ("harry", "7")]

This could come from CSV of course, it doesn't matter to the sorting. 当然,这可能来自CSV,与排序无关。 You can sort just by calling sorted() : 您可以通过调用sorted()

sorted(data, key = lambda x: int(x[1]))

The lambda is a function that picks the second element of each sub-list as the key, ie the score, and converts it to a number for sorting. lambda是一种功能,它选择每个子列表的第二个元素作为键(即得分),并将其转换为数字以进行排序。 This prints: 打印:

[('john', '6'), ('harry', '7'), ('fred', '8'), ('josh', '12')]

You could do something like this: ( Create a dictionary out of your values ) 您可以执行以下操作:(根据您的值创建字典)

for row in reader:
    my_dict = {row[0]:row[1]}

Then you can do a representation of a sorted dictionary (dictionaries are inherently orderless so this will be a list): 然后,您可以表示已排序的字典(字典本质上是无序的,因此这将是一个列表):

import operator
sorted_dict = sorted(my_dict.items(), key=operator.itemgetter(1))

It's worth noting that there are better / simpler ways to do this ( Panda for instance ) but atleast you learn a different approach :) 值得注意的是,有更好/更简单的方法可以做到这一点(例如Panda),但您至少会学到另一种方法:)

If all your CSV contains is a name and a number and your names are unique, then 如果您的CSV包含一个名称和一个数字,并且您的名称是唯一的,则

  1. store CSV contents as {name:score} as a dict 将CSV内容存储为{name:score}作为字典
  2. Use the code below to sort based on values(scores in your case) 使用下面的代码基于值进行排序(您的情况是分数)

     import operator x = {"josh": 12, "john": 6, "fred": 8, "harry": 7,} sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1)) 
from operator import itemgetter
result = []
with open("data", 'r') as f:
        r = csv.reader(f, delimiter=' ')
        # next(r, None)  # skip the headers
        for row in r:
            result.append(row[:-1])
# sort by numeric part, which is a key value for sorted.
# itemgetter(1) gets the number in each sublist
print(sorted(result,key=itemgetter(1)))

[['josh', '12'], ['john', '6'], ['harry', '7'], ['fred', '8']]

You can utilize pandas for this. 您可以为此使用熊猫

import pandas as pd

df = pd.read_csv('students.csv', header=None)
df.columns = ['Name', 'Score']
df.sort('Score', ascending=False, inplace=True)

At the end of this, you will have a data frame that looks like this: 最后,您将获得一个数据框,如下所示:

    Name  Score
0   josh     12
2   fred      8
3  harry      7
1   john      6

The code is reading your CSV file, and explicitly stating there isn't a header. 该代码正在读取您的CSV文件,并明确指出没有标题。 By default pandas assumes that the first row contains column headers. 默认情况下,pandas假定第一行包含列标题。 Since there isn't any headers, we then add those: Name and Score . 由于没有任何标题,因此我们添加了它们: NameScore Finally, we sort, inplace, based on the Score column. 最后,我们根据“ Score列进行就地排序。 You could leave the original dataframe unchanged by removing the inplace= parameter and doing this: 您可以通过删除inplace=参数并执行以下操作来保持原始数据框不变:

sorted_df = df.sort('Score', ascending=False)

After this line, you'd have your original file in df and the sorted file in sorted_df 在此行之后,您将在df拥有原始文件,在sorted_df拥有已排序文件

If your data in the csv file looks like this: 如果您的csv文件中的data如下所示:

josh 12
john 6
fred 8
harry 7

Then you can create a dictionary and use key=d.__getitem__ : 然后,您可以创建dictionary并使用key=d.__getitem__

import csv
with open('yourfile.csv', 'rb') as f:
    reader = csv.reader(f)
    d = {}
    for row in f:
        row = row.split(",")
        d[row[0]]=int(row[1]) 
k = sorted(d, key=d.__getitem__, reverse=True)
v = sorted(d.values(), reverse=True)
sorted_d = zip(k,v)
print (sorted_d)

Output: 输出:

[('josh', 12), ('fred', 8), ('harry', 7), ('john', 6)]

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

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