網(wǎng)站首頁 編程語言 正文
參數(shù)解釋
DataFrame.sort_values(by,
axis=0,
ascending=True,
inplace=False,
kind='quicksort',
na_position='last', # last,first;默認(rèn)是last
ignore_index=False,
key=None)
參數(shù)的具體解釋為:
- by:表示根據(jù)什么字段或者索引進(jìn)行排序,可以是一個或多個
- axis:排序是在橫軸還是縱軸,默認(rèn)是縱軸axis=0
- ascending:排序結(jié)果是升序還是降序,默認(rèn)是升序
- inplace:表示排序的結(jié)果是直接在原數(shù)據(jù)上的就地修改還是生成新的DatFrame
- kind:表示使用排序的算法,快排quicksort,,歸并mergesort, 堆排序heapsort,穩(wěn)定排序stable ,默認(rèn)是 :快排quicksort
- na_position:缺失值的位置處理,默認(rèn)是最后,另一個選擇是首位
- ignore_index:新生成的數(shù)據(jù)幀的索引是否重排,默認(rèn)False(采用原數(shù)據(jù)的索引)
- key:排序之前使用的函數(shù)
數(shù)據(jù)值的排序主要使用sort_values(),數(shù)字按大小排序,字符按字母順序
Series和DataFrame都支持此方法
import pandas as pd
df = pd.DataFrame([['liver','E',89,21,24,64],
['Arry','C',36,37,37,57],
['Ack','A',57,60,18,84],
['Eorge','C',93,96,71,78],
['Oah','D',65,49,61,86]
],
columns = ['name','team','Q1','Q2','Q3','Q4'])
res1 = df.Q1.sort_values()
# DataFrame 需要傳入一個或多個排序的列名
res2 = df.sort_values('Q4')
# 默認(rèn)排序是升序,但可以指定排序方式
# 下例先按team升序排列,如遇到相同的team再按name降序排列
res3 = df.sort_values(by = ['team','name'], ascending = [True, False])
結(jié)果展示
df
res1
res2
res3
擴(kuò)展
# 其他常用方法如下:
s.sort_values(ascending = False) # 降序
s.sort_values(inplace = True) # 修改生效
s.sort_values(na_position = 'first') # 空值在前
# df按指定字段排列
df.sort_values(by = ['team'])
df.sort_values('Q1')
# 按多個字段,先排team,在同team內(nèi)再看Q1
df.sort_values(by = ['mean','Q1'])
# 全降序
df.sort_values(by = ['mean','Q1'], ascending = False)
# 對應(yīng)指定team升Q1降
df.sort_values(by = ['mean','Q1'], ascending = [True, False])
原文鏈接:https://blog.csdn.net/Hudas/article/details/122921600
相關(guān)推薦
- 2022-05-12 Kotlin 接口 interface 默認(rèn)實現(xiàn)了open。并且可以提供默認(rèn)實現(xiàn)
- 2023-01-18 Python3中安裝后SSL問題及解決_python
- 2022-02-03 ionic錨點(diǎn)操作
- 2022-06-02 Pandas數(shù)據(jù)結(jié)構(gòu)之Series的使用_python
- 2022-06-12 PostgreSQL數(shù)據(jù)庫視圖及子查詢使用操作_PostgreSQL
- 2022-09-03 pandas?如何保存數(shù)據(jù)到excel,csv_python
- 2022-04-28 WPF簡介與基礎(chǔ)開發(fā)_實用技巧
- 2022-10-16 python?sys模塊使用方法介紹_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支