我有一个csv文件&想要按照整数而不是像itemgetter那样的字符串对它进行排序

时间:2015-06-30 00:15:47

标签: python sorting csv integer multiple-columns

x   y   xm  ym  mdb wat tile
43460   2095    623424.9213 -3891371.696    1   10324   38
43010   2599    638544.9213 -3877871.696    1   1   38
35871   2702    641634.9213 -3663701.696    1   0   50
43451   3296    659454.9213 -3891101.696    1   5951    38
40081   3330    660474.9213 -3790001.696    1   0   38
39084   3796    674454.9213 -3760091.696    1   0   50
6910    34119   1584144.921 -2794871.696    1   0   128
7040    29565   1447524.921 -2798771.696    1   0   127
7452    27335   1380624.921 -2811131.696    1   0   127
7976    34974   1609794.921 -2826851.696    1   0   128

1 个答案:

答案 0 :(得分:0)

pandas让这很容易。

import pandas as pd

# Read in the csv file
df = pd.read_csv('input_file.csv')

>>> df

       x      y            xm           ym  mdb    wat  tile
0  43460   2095   623424.9213 -3891371.696    1  10324    38
1  43010   2599   638544.9213 -3877871.696    1      1    38
2  35871   2702   641634.9213 -3663701.696    1      0    50
3  43451   3296   659454.9213 -3891101.696    1   5951    38
4  40081   3330   660474.9213 -3790001.696    1      0    38
5  39084   3796   674454.9213 -3760091.696    1      0    50
6   6910  34119  1584144.9210 -2794871.696    1      0   128
7   7040  29565  1447524.9210 -2798771.696    1      0   127
8   7452  27335  1380624.9210 -2811131.696    1      0   127
9   7976  34974  1609794.9210 -2826851.696    1      0   128

# Do the actual sort. I've chosen x and y for sorting here, arbitrarily as an example
df_sorted = df.sort(['x','y'], ascending=[1,0]) # This sorts column x ascending and y descending

>>> df_sorted

       x      y            xm           ym  mdb    wat  tile
6   6910  34119  1584144.9210 -2794871.696    1      0   128
7   7040  29565  1447524.9210 -2798771.696    1      0   127
8   7452  27335  1380624.9210 -2811131.696    1      0   127
9   7976  34974  1609794.9210 -2826851.696    1      0   128
2  35871   2702   641634.9213 -3663701.696    1      0    50
5  39084   3796   674454.9213 -3760091.696    1      0    50
4  40081   3330   660474.9213 -3790001.696    1      0    38
1  43010   2599   638544.9213 -3877871.696    1      1    38
3  43451   3296   659454.9213 -3891101.696    1   5951    38
0  43460   2095   623424.9213 -3891371.696    1  10324    38


# Write output to csv if you want
df_sorted.to_csv('output_csv.csv', index=False)