我的数据集
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列中的值进行排序。我该怎么办?
最佳答案
我认为您需要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
关于python - sort_values,TypeError:“numpy.ndarray”和“str”的实例之间不支持“<”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44846824/