sort_values,TypeError:'<' 'numpy.ndarray'和'str'的实例之间不支持

时间:2017-06-30 12:45:18

标签: python pandas

我的数据集

Mimi: 47.20
Marko: 51.14
Shellie: 49.95
Lopes: 48.80
Jack: 46.60
Neli: 52.70
Martin: 57.65
Jessi: 55.45
Adri: 52.30
Lia: 59.90

我的代码

import pandas as pd
df = pd.read_csv('laptimes.txt', sep=":", header = None)
print (df)
newdata = df.sort_values(by=1, axis=1, ascending=True)
print (newdata)

但我得到了这个

Traceback (most recent call last):
  File "o4.py", line 4, in <module>
    newdata = df.sort_values(by=1, axis=1, ascending=True)
  File "/home/milenko/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3299, in sort_values
    na_position=na_position)
  File "/home/milenko/anaconda3/lib/python3.6/site-packages/pandas/core/sorting.py", line 247, in nargsort
    indexer = non_nan_idx[non_nans.argsort(kind=kind)]
TypeError: '<' not supported between instances of 'numpy.ndarray' and 'str'

我真正想要的是在第1列中对值进行排序。我该怎么做?

1 个答案:

答案 0 :(得分:1)

我认为您需要axis=0,对于按列排序,什么是默认参数,因此可以省略。同样ascending=True

newdata = df.sort_values(by=1)

axis=1用于每行排序:

df = pd.DataFrame({0: [7, 2], 1: [3, 5], 2: [4, 8]})
print (df)
   0  1  2
0  7  3  4
1  2  5  8

#sort by first row 0
print (df.sort_values(by=0, axis=1))
   1  2  0
0  3  4  7
1  5  8  2

#sort by first column 0
print (df.sort_values(by=0, axis=0))
   0  1  2
1  2  5  8
0  7  3  4

#sort by first column 0
print (df.sort_values(by=0))
   0  1  2
1  2  5  8
0  7  3  4