Python数据分析 知识量:13 - 56 - 232
按照单列进行排序时,需要使用sort_values()函数,通过参数by和ascending指定排序的列名和升(降)序。当ascending=False时表示按照降序排列;当ascending=True或省略时(默认情况下),按照升序排列。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam.xlsx") print(df,'\n') print(df.sort_values(by=['Chinese']),'\n') # 默认按照升序排列 print(df.sort_values(by=['Chinese'],ascending=False)) # 指定按照降序排列
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Noah male 90 50 66 3 Olivia female 86 87 44 4 Liam male 55 88 69 5 Sophia female 90 66 96 6 Liam male 55 88 69 7 Isabella female 66 85 55 Name Sex Chinese English Math 4 Liam male 55 88 69 6 Liam male 55 88 69 1 Emma female 56 56 55 7 Isabella female 66 85 55 3 Olivia female 86 87 44 0 Noah male 90 50 66 2 Noah male 90 50 66 5 Sophia female 90 66 96 Name Sex Chinese English Math 0 Noah male 90 50 66 2 Noah male 90 50 66 5 Sophia female 90 66 96 3 Olivia female 86 87 44 7 Isabella female 66 85 55 1 Emma female 56 56 55 4 Liam male 55 88 69 6 Liam male 55 88 69
当数据对象中含有缺失值时,默认情况下,缺失值显示在最后面。也可以通过参数na_position进行调整。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_nan.xlsx") print(df,'\n') print(df.sort_values(by=['Chinese']),'\n') # 含缺失值的默认排序 print(df.sort_values(by=['Chinese'],na_position='first')) # 缺失值排在最前面
运行结果为:
Name Sex Chinese English Math 0 Noah male 90.0 50.0 66.0 1 Emma NaN 56.0 56.0 55.0 2 NaN NaN NaN NaN NaN 3 Olivia female 86.0 87.0 NaN 4 Liam male 55.0 NaN 69.0 5 Sophia female 90.0 66.0 96.0 6 Liam male 55.0 NaN 69.0 7 Isabella female NaN 85.0 55.0 Name Sex Chinese English Math 4 Liam male 55.0 NaN 69.0 6 Liam male 55.0 NaN 69.0 1 Emma NaN 56.0 56.0 55.0 3 Olivia female 86.0 87.0 NaN 0 Noah male 90.0 50.0 66.0 5 Sophia female 90.0 66.0 96.0 2 NaN NaN NaN NaN NaN 7 Isabella female NaN 85.0 55.0 Name Sex Chinese English Math 2 NaN NaN NaN NaN NaN 7 Isabella female NaN 85.0 55.0 4 Liam male 55.0 NaN 69.0 6 Liam male 55.0 NaN 69.0 1 Emma NaN 56.0 56.0 55.0 3 Olivia female 86.0 87.0 NaN 0 Noah male 90.0 50.0 66.0 5 Sophia female 90.0 66.0 96.0
参数na_position的默认值为last。
同时依据多个列进行排序同样利用sort_values()函数来实现,但需要以列表的形式给出排序的多个列名及升(降)序。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam.xlsx") print(df,'\n') print(df.sort_values(by=['Chinese','English'],ascending=[True,False]))
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Noah male 90 50 66 3 Olivia female 86 87 44 4 Liam male 55 88 69 5 Sophia female 90 66 96 6 Liam male 55 88 69 7 Isabella female 66 85 55 Name Sex Chinese English Math 4 Liam male 55 88 69 6 Liam male 55 88 69 1 Emma female 56 56 55 7 Isabella female 66 85 55 3 Olivia female 86 87 44 5 Sophia female 90 66 96 0 Noah male 90 50 66 2 Noah male 90 50 66
以上示例中,先按照Chinese列升序排列,Chinese列的值相同的再按照English列降序排列,因此,Sophia排在Noah的前面。
Copyright © 2017-Now pnotes.cn. All Rights Reserved.
编程学习笔记 保留所有权利
MARK:3.0.0.20240214.P35
From 2017.2.6