import pandas as pd
df = pd.DataFrame({"one": [2, 1, 1, 1], "two": [1, 3, 2, 4], "three": [5, 4, 3, 2]})
print(df)
df为:
print(df.sort_values(by="two")) # 默认升序排列
by="two"意思是指定two字段排序
输出为:
print(df.sort_values(by="two", ascending=False))
# ascending=False为降序排列,ascending=True为升序排列,不指定则为升序排列
输出为:
多个字段排序
# 指定按照one和two两个字段排序,第一个字段升序,第二个字段降序
print(df.sort_values(by=["one", "two"], ascending=[True, False]))
输出为: