如何使用python对不带标题的csv文件进行排序?

时间:2018-08-12 06:37:22

标签: python python-2.7 pandas pandas-groupby

如何使用python pandas对没有标题的csv文件进行排序? 注意:csv文件没有标题。

我的文件:

1,a123,adam,student
2,b345,becky,student
3,c678,charles,teacher
1,d987,dickson,teacher
2,e654,evanston,teacher

预期输出:

1,a123,adam,student
1,d987,dickson,teacher
2,b345,becky,student
2,e654,evanston,teacher
3,c678,charles,teacher

1 个答案:

答案 0 :(得分:1)

read_csv与参数names一起用于DataframeDataFrame.sort_values的新列名称:

import pandas as pd

temp=u"""1,a123,adam,student

2,b345,becky,student

3,c678,charles,teacher

1,d987,dickson,teacher

2,e654,evanston,teacher"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), names=['a','b','c','d'])

print (df)
   a     b         c        d
0  1  a123      adam  student
1  2  b345     becky  student
2  3  c678   charles  teacher
3  1  d987   dickson  teacher
4  2  e654  evanston  teacher

df = df.sort_values('a')
print (df)
   a     b         c        d
0  1  a123      adam  student
3  1  d987   dickson  teacher
1  2  b345     becky  student
4  2  e654  evanston  teacher
2  3  c678   charles  teacher

或使用header=None作为默认列名称-RangeIndex

df = pd.read_csv(pd.compat.StringIO(temp), header=None)

print (df)
   0     1         2        3
0  1  a123      adam  student
1  2  b345     becky  student
2  3  c678   charles  teacher
3  1  d987   dickson  teacher
4  2  e654  evanston  teacher

df = df.sort_values(0)
print (df)
   0     1         2        3
0  1  a123      adam  student
3  1  d987   dickson  teacher
1  2  b345     becky  student
4  2  e654  evanston  teacher
2  3  c678   charles  teacher