和sort_index()方法不一样的是,sort_values()方法可以指定排序是根据哪一列的内容,当然,我们需要在括号中填写那一列的参数,用by作为参数关键词传入。比如我们按照分数(score)降序排列,就可以编写以下程序:
import pandas as pd
dict_data = {
'score' :[95,98,92,89,82,93],
'gender':['M','F','M',"M","F","M"]
}
DF_data = pd.DataFrame(dict_data,
columns=['gender','hometown','score'],
index=["Li Lei","Han Meimei","Tom","Adam","Betty","Curry"])
DF_data = DF_data.fillna({'hometown':'unknown'})
print(DF_data.sort_values(by = 'score', ascending = False))
我们将得到以下输出结果:
gender hometown score
Han Meimei F unknown 98
Li Lei M unknown 95
Curry M unknown 93
Tom M unknown 92
Adam M unknown 89
Betty F unknown 82
[Finished in 3.8s]