網站首頁 編程語言 正文
前言:
pandas中排序的幾種常用方法,主要包括sort_index和sort_values。
基礎數據:
import pandas as pd
import numpy as np
data = {
'brand':['Python', 'C', 'C++', 'C#', 'Java'],
'B':[4,6,8,12,10],
'A':[10,2,5,20,16],
'D':[6,18,14,6,12],
'years':[4,1,1,30,30],
'C':[8,12,18,8,2]
}
index = [9,3,4,5,2]
df = pd.DataFrame(data=data, index = index)
print("df數據:\n", df, '\n')
out:
df數據:
? ? ?A ? B ? C ? D ? brand ?years
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30?
按行索引排序:
print("按行索引排序:\n", df.sort_index(), '\n')
out:
按行索引排序:
? ? ?A ? B ? C ? D ? brand ?years
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
通過設置參數ascending可以設置升序或者降序排序,默認情況下ascending=True,為升序排序。
設置ascending=False時,為降序排序。
print("按行索引降序排序:\n", df.sort_index(ascending=False), '\n')
out:
按行索引降序排序:
? ? ?A ? B ? C ? D ? brand ?years
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30
按列的名稱排序:
設置參數axis=1實現按列的名稱排序:
print("按列名稱排序:\n", df.sort_index(axis=1), '\n')
out:
按列名稱排序:
? ? ?A ? B ? C ? D ? brand ?years
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30
同樣,也可以設置ascending參數:
print("按列名稱排序:\n", df.sort_index(axis=1, ascending=False), '\n')
out:
按列名稱排序:
? ? years ? brand ? D ? C ? B ? A
9 ? ? ?4 ?Python ? 6 ? 8 ? 4 ?10
3 ? ? ?1 ? ? ? C ?18 ?12 ? 6 ? 2
4 ? ? ?1 ? ? C++ ?14 ?18 ? 8 ? 5
5 ? ? 30 ? ? ?C# ? 6 ? 8 ?12 ?20
2 ? ? 30 ? ?Java ?12 ? 2 ?10 ?16
按數值排序:
sort_values()是pandas中按數值排序的函數:
1、按單個列的值排序
sort_values()中設置單個列的列名,可以對單個列進行排序,通過設置ascending可以設置升序或者降序。
print("按列名稱A排序:\n", df.sort_values('A'), '\n')
out:
按列名稱排序:
? ? ?A ? B ? C ? D ? brand ?years
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30
設置ascending=False進行降序排序:
print("按列名稱A降序排序:\n", df.sort_values('A', ascending=False), '\n')
out:
按列名稱A降序排序:
? ? ?A ? B ? C ? D ? brand ?years
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
按多個列的值排序:
先按year列的數據進行升序排序,year列相同的再看B列進行升序排序
print("按多個列排序:\n", df.sort_values(['years', 'B']), '\n')
out:
按多個列排序:
? ? ?A ? B ? C ? D ? brand ?years
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30?
也可以分別設置列的升序、降序來排序:
years列為升序,B列為降序。
print("按多個列排序:\n", df.sort_values(['years', 'B'], ascending=[True, False]), '\n')
out:
按多個列排序:
? ? ?A ? B ? C ? D ? brand ?years
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30
inplace使用:
inplace=True:不創建新的對象,直接對原始對象進行修改;默認是False,即創建新的對象進行修改,原對象不變,和深復制和淺復制有些類似。
df.sort_values('A', inplace=True)
print("按A列排序:\n", df, '\n')
out:
按A列排序:
? ? ?A ? B ? C ? D ? brand ?years
3 ? 2 ? 6 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8 ?18 ?14 ? ? C++ ? ? ?1
9 ?10 ? 4 ? 8 ? 6 ?Python ? ? ?4
2 ?16 ?10 ? 2 ?12 ? ?Java ? ? 30
5 ?20 ?12 ? 8 ? 6 ? ? ?C# ? ? 30
缺失值:
含有nan值的數據排序:
data = {
'brand':['Python', 'C', 'C++', 'C#', 'Java'],
'B':[4,6,8,np.nan,10],
'A':[10,2,5,20,16],
'D':[6,18,14,6,12],
'years':[4,1,1,30,30],
'C':[8,12,18,8,2]
}
index = [9,3,4,5,2]
df = pd.DataFrame(data=data, index = index)
print("df數據:\n", df, '\n')
out:
df數據:
? ? ?A ? ? B ? C ? D ? brand ?years
9 ?10 ? 4.0 ? 8 ? 6 ?Python ? ? ?4
3 ? 2 ? 6.0 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8.0 ?18 ?14 ? ? C++ ? ? ?1
5 ?20 ? NaN ? 8 ? 6 ? ? ?C# ? ? 30
2 ?16 ?10.0 ? 2 ?12 ? ?Java ? ? 30
B列含有nan值,對B列進行排序,缺失值排在最前面:
print("按B列排序:\n", df.sort_values('B', na_position='first'), '\n')
按B列排序:
? ? ?A ? ? B ? C ? D ? brand ?years
5 ?20 ? NaN ? 8 ? 6 ? ? ?C# ? ? 30
9 ?10 ? 4.0 ? 8 ? 6 ?Python ? ? ?4
3 ? 2 ? 6.0 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8.0 ?18 ?14 ? ? C++ ? ? ?1
2 ?16 ?10.0 ? 2 ?12 ? ?Java ? ? 30
包含缺失值,缺失值排在最后:
print("按B列排序:\n", df.sort_values('B', na_position='last'), '\n')
out:
按B列排序:
? ? ?A ? ? B ? C ? D ? brand ?years
9 ?10 ? 4.0 ? 8 ? 6 ?Python ? ? ?4
3 ? 2 ? 6.0 ?12 ?18 ? ? ? C ? ? ?1
4 ? 5 ? 8.0 ?18 ?14 ? ? C++ ? ? ?1
2 ?16 ?10.0 ? 2 ?12 ? ?Java ? ? 30
5 ?20 ? NaN ? 8 ? 6 ? ? ?C# ? ? 30
原文鏈接:https://blog.csdn.net/xiadeliang1111/article/details/126831607
相關推薦
- 2022-06-21 Windows下安裝Git_其它綜合
- 2023-10-31 SpringBoot手動獲取實例
- 2022-03-15 When allowCredentials is true, allowedOrigins cann
- 2022-12-05 python實現兩字符串映射_python
- 2023-01-14 詳解C#如何實現屏幕放大和取色功能_C#教程
- 2022-09-25 python mac版本解釋器安裝
- 2022-07-24 Golang實現文件夾的創建與刪除的方法詳解_Golang
- 2023-03-11 go-micro微服務JWT跨域認證問題_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支