网站首页 > 教程分享 正文
来源:AI入门学习
作者:小伍哥
数据排序,是使用非常高频的功能,Pandas排序支持做的非常好,主要涉及两个函数,两种数据类型,组合起来四种情况。
Series排序
- Series.sort_index 索引排序
- Series.sort_values 值引排序
DataFrame排序
- DataFrame.sort_index 索引排序
- DataFrame.sort_values 值引排序
一、Series的排序
1、sort_index 索引排序
定义一个Series用于实验
s = Series([4,1,2,3],index=['d','a','c','b'])
d 4
a 1
c 2
b 3
对Series的索引进行升序排序,默认即可,无需使用其他参数
s.sort_index()
a 1
b 3
c 2
d 4
对Series的索引进行降序排序,使用ascending=False参数
s.sort_index(ascending=False)
d 4
c 2
b 3
a 1
2、sort_values 值引排序
用 法:
Series.sort_values(ascending=True, inplace=Flase)
参数:
- ascending:默认为True升序排列,为Flase降序排序
- inplace:是否修改原始的Series
对Series的值进行升序排序,默认即可,无需使用其他参数
s.sort_values()
a 1
c 2
b 3
d 4
对Series的值进行降序排序,使用ascending=False参数
s.sort_values(ascending=False)
d 4
b 3
c 2
a 1
二、 DataFrame的排序
1、sort_index 索引排序
DataFrame.sort_index(by=None,
axis=0, level=None,
ascending=True,
inplace=False,
kind='quicksort',
na_position='last',
sort_remaining=True)
- by:按照某一列或几列数据进行排序,但是by参数貌似不建议使用
- axis:0按照行名排序;1按照列名排序
- level:默认None,否则按照给定的level顺序排列---貌似并不是,文档
- ascending:默认True升序排列;False降序排列
- inplace:默认False,否则排序之后的数据直接替换原来的数据框
- kind:排序方法,{‘quicksort’, ‘mergesort’, ‘heapsort’}, default ‘quicksort’。似乎不用太关心。
- na_position:缺失值默认排在最后{"first","last"}
构建数据集
import numpy as np
import pandas as pd
data = pd.DataFrame( np.arange(9).reshape(3,3),
index = ["0","2","1"],
columns = ["col_a","col_c","col_b"])
data
col_a col_c col_b
0 0 1 2
2 3 4 5
1 6 7 8
按行的索引升序进行排序,默认按行,升序
data.sort_index()
col_a col_c col_b
0 0 1 2
1 6 7 8
2 3 4 5
按行的索引按降序进行排序
data.sort_index(ascending=False)
col_a col_c col_b
2 3 4 5
1 6 7 8
0 0 1 2
按列升序的索引进行排序
data.sort_index(axis=1)
Out[10]:
col_a col_c col_b
0 0 1 2
1 6 7 8
2 3 4 5
2、sort_values 值引排序
用 法:
DataFrame.sort_values(
by,
axis=0,
ascending=True,
inplace=False,
kind='quicksort',
na_position='last')
参 数:
- by:字符串或者List<字符串>;如果axis=0,那么by="列名";如果axis=1,那么by="行名"。
- axis:{0 or ‘index’, 1 or ‘columns’}, default 0,默认按照列排序,即纵向排序;如果为1,则是横向排序。
- ascending:布尔型,True则升序,如果by=['列名1','列名2'],则该参数可以是[True, False],即第一字段升序,第二个降序。
- inplace:布尔型,是否用排序后的数据框替换现有的数据框。
- kind:排序方法,{‘quicksort’, ‘mergesort’, ‘heapsort’}, default ‘quicksort’。似乎不用太关心。
- na_position:{‘first’, ‘last’}, default ‘last’,默认缺失值排在最后面。
构建实验用数据
data =pd.DataFrame([[2,3,12],[6,2,8],[9,5,7]],
index=["0", "2", "1"],
columns=["col_a", "col_c", "col_b"])
col_a col_c col_b
0 2 3 12
2 6 2 8
1 9 5 7
按指定列的值大小顺序进行排序
data.sort_values(by='col_c')
col_a col_c col_b
2 6 2 8
0 2 3 12
1 9 5 7
按多列进行排序
data.sort_values(by=['col_b','col_a'])
col_a col_c col_b
1 9 5 7
2 6 2 8
0 2 3 12
先按col_b列降序,再按col_a列升序排序
data.sort_values(by=['col_b','col_a'],axis=0,ascending=[False,True])
col_a col_c col_b
0 2 3 12
2 6 2 8
1 9 5 7
按行升序排列
data.sort_values(by='2',axis=1)
col_c col_a col_b
0 3 2 12
2 2 6 8
1 5 9 7
按 2行 升序,0行降排列
data.sort_values(by=['2','0'],axis=1)
col_c col_a col_b
0 3 2 12
2 2 6 8
1 5 9 7
猜你喜欢
- 2024-10-11 Java中Arrays的两种排序方法(sort和parallelSort)比较
- 2024-10-11 Excel中的排序函数RANK,这6种使用技巧你一定不能错过
- 2024-10-11 七种排序算法 冒泡,选择,插入,希尔,快速,归并,堆
- 2024-10-11 深入浅出Redis:Redis的排序命令Sort
- 2024-10-11 「收藏」JS数组排序技巧汇总(冒泡、sort、快速、希尔等排序)
- 2024-10-11 R语言sort和order排序函数(r语言给数据排序)
- 2024-10-11 vba如何通过调用sort方法实现排序?跟着文章解开心中的谜题!
- 2024-10-11 用SORTBY函数进行多条件排序 #excel技巧
- 2024-10-11 python数据排序-sorted与sort(python中的sorted排序)
- 2024-10-11 Linux基础知识之sort排序查看硬盘SN
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)