简体   繁体   English

如何在python中将列转换为数值以进行排序

[英]How to convert the column to numeric in python for sorting

I am new to python (learner). 我是python(学习者)的新手。 Please check my question and help me to resolve the issue. 请检查我的问题,并帮助我解决问题。

I have csv file with the below content 我有以下内容的csv文件

test,cycle,date,status
func,2,09/07/17,pass
func,10,09/08/17,fail
func,3,09/08/17,pass
func,1,09/08/17,no run
func,22,09/08/17,in progress
func,11,09/08/17,on hold

when i sort 2nd column (cycle) it shows the below output 当我对第二列(循环)进行排序时,它显示以下输出

['func', '1', '09/08/17', 'no run']
['func', '10', '09/08/17', 'fail']
['func', '11', '09/08/17', 'on hold']
['func', '2', '09/07/17', 'pass']
['func', '22', '09/08/17', 'in progress']
['func', '3', '09/08/17', 'pass']

Problem I faced here is it is sorting as string, due to this it shows the output as 1, 10, 11, 2, 22, 3. but i want to get the output in sorted by numeric (int/float) so that i will get the output 1, 2, 3, 10, 11, 22. 我在这里遇到的问题是它按字符串排序,因此它显示输出为1、10、11、2、22、3。但是我想按数字(整数/浮点数)对输出进行排序,以便我将获得输出1,2,3,10,11,22

Below is the small script i have. 下面是我的小脚本。 could you help me to modify the script to change the column it to numeric before sort? 您能帮我修改脚本,以便在排序之前将其列更改为数字吗?

with open ('C:\Automation\sample.csv') as csvfile:

readCSVfile = csv.reader(csvfile, delimiter=',') readCSVfile = csv.reader(csvfile,delimiter =',')

for row in readCSVfile:
sort = sorted(readCSVfile, key=operator.itemgetter(1), reverse = False)
 for eachline in sort:
print eachline`

You could pre-process the lines as you read them in: 您可以在阅读以下内容时对其进行预处理:

#!python2
import csv
import operator

with open ('sample.csv','rb') as csvfile:
    readCSVfile = csv.reader(csvfile)
    header = next(readCSVfile)
    rows = []
    for row in readCSVfile:
        test,cycle,date,status = row
        rows.append([test,int(cycle),date,status])
rows.sort(key=operator.itemgetter(1))
for row in rows:
    print row

Output: 输出:

['func', 1, '09/08/17', 'no run']
['func', 2, '09/07/17', 'pass']
['func', 3, '09/08/17', 'pass']
['func', 10, '09/08/17', 'fail']
['func', 11, '09/08/17', 'on hold']
['func', 22, '09/08/17', 'in progress']

You could also use a different sort key, leaving the column a string: 您还可以使用其他排序键,将列保留为字符串:

#!python2
import csv
import operator

with open ('sample.csv','rb') as csvfile:
    readCSVfile = csv.reader(csvfile)
    header = next(readCSVfile)
    rows = [row for row in readCSVfile]
rows.sort(key=lambda row: int(row[1]))
for row in rows:
    print row

Output: 输出:

['func', '1', '09/08/17', 'no run']
['func', '2', '09/07/17', 'pass']
['func', '3', '09/08/17', 'pass']
['func', '10', '09/08/17', 'fail']
['func', '11', '09/08/17', 'on hold']
['func', '22', '09/08/17', 'in progress']

Then you have to convert it to numeric. 然后,您必须将其转换为数字。 Python csv module do not auto recognize data types. Python csv模块无法自动识别数据类型。

You can do it by something like: 您可以通过类似的方法来做到这一点:

numberedCSV = []
for row in readCSVfile:
    row[1] = int(row[1])
    numberedCSV.append(row)

Then perform sorting on the numberedCSV . 然后对numberedCSV进行排序。

btw, I do not understand your intention the code you posted. 顺便说一句,我不明白您打算发布的代码。 Why do you need two loops? 为什么需要两个循环?

Here this might be what you are looking for. 这可能是您要寻找的。

    # take second element for sort
def takeSecond(elem):
    return int(elem[1])

# random list
stuff = [['func', '1', '09/08/17', 'no run'],
 ['func', '10', '09/08/17', 'fail'],
 ['func', '11', '09/08/17', 'on hold'],
 ['func', '2', '09/07/17', 'pass'],
 ['func', '22', '09/08/17', 'in progress'],
 ['func', '3', '09/08/17', 'pass']]

# sort list with key
sortedList = sorted(stuff, key=takeSecond)

# print list
print('Sorted list:', sortedList)

cheers. 干杯。

As other answers has stated, either you can 正如其他答案所说,您可以

  • using another function than operator.itemgetter to convert value to int when sorting 在排序时使用不是operator.itemgetter另一个函数将值转换为int
  • or using a for loop to convert the array data before sorting. 或使用for循环在排序之前转换数组数据。

But if you working with tabular data like this often, it's better to use pandas . 但是,如果经常使用这种表格数据,最好使用pandas You need to install it, but again: if you do this often, it's worth it. 您需要安装它,但是再次:如果经常执行此操作,那是值得的。

import pandas as pd

df = pd.read_csv('sample.csv')

df['cycle'] = df['cycle'].astype(int)

print(df.sort_values(by='cycle'))

# or reverse
print(df.sort_values(by='cycle', ascending=False))

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

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