如何使用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
答案 0 :(得分:1)
将read_csv
与参数names
一起用于Dataframe
和DataFrame.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