python pandas 数据排序的几种常用方法

作者:soulsoul_god 时间:2021-10-02 06:25:01 

前言:

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

标签:python,pandas,数据,排序
0
投稿

猜你喜欢

  • 用header 发送cookie的php代码

    2023-07-11 11:15:06
  • python逐行读取文件内容的三种方法

    2023-01-05 14:07:30
  • 一组常用的弹出窗口用法总结

    2007-10-08 13:04:00
  • Linux+php+apache+oracle环境搭建之CentOS下安装Oracle数据库

    2023-10-08 01:02:56
  • Gregarius中文日期格式问题解决办法

    2023-11-18 09:51:00
  • Python实战之设计一个多功能办公小工具

    2023-05-26 02:54:11
  • OpenCV-Python实现通用形态学函数

    2022-02-13 18:10:33
  • Python中类型关系和继承关系实例详解

    2023-10-08 06:15:06
  • 用python统计代码行的示例(包括空行和注释)

    2022-06-28 02:15:30
  • Python实现生成随机日期字符串的方法示例

    2023-10-27 11:51:37
  • 跟老齐学Python之正规地说一句话

    2022-10-15 06:49:29
  • ASP如何输出字符

    2007-09-22 18:41:00
  • python实现WebSocket服务端过程解析

    2022-09-14 10:45:19
  • 将图片文件嵌入到wxpython代码中的实现方法

    2023-04-23 06:52:20
  • python使用jieba实现中文分词去停用词方法示例

    2021-02-04 11:27:17
  • php防止sql注入示例分析和几种常见攻击正则表达式

    2023-11-23 17:01:02
  • python中最小二乘法详细讲解

    2022-02-12 22:33:07
  • python实现滑雪游戏

    2021-10-08 05:20:35
  • 如何用Frontpage下载别人的网站模板

    2008-03-03 12:58:00
  • Python包和模块的分发详细介绍

    2021-10-06 07:25:32
  • asp之家 网络编程 m.aspxhome.com