• python pandas 数据排序


    pandas中排序的几种常用方法,主要包括sort_index和sort_values。

    基础数据:

    1. import pandas as pd
    2. import numpy as np
    3. data = {
    4. 'brand':['Python', 'C', 'C++', 'C#', 'Java'],
    5. 'B':[4,6,8,12,10],
    6. 'A':[10,2,5,20,16],
    7. 'D':[6,18,14,6,12],
    8. 'years':[4,1,1,30,30],
    9. 'C':[8,12,18,8,2]
    10. }
    11. index = [9,3,4,5,2]
    12. df = pd.DataFrame(data=data, index = index)
    13. print("df数据:\n", df, '\n')

    out:

    1. df数据:
    2. A B C D brand years
    3. 9 10 4 8 6 Python 4
    4. 3 2 6 12 18 C 1
    5. 4 5 8 18 14 C++ 1
    6. 5 20 12 8 6 C# 30
    7. 2 16 10 2 12 Java 30

    按行索引排序:

    print("按行索引排序:\n", df.sort_index(), '\n')

    out:

    1. 按行索引排序:
    2. A B C D brand years
    3. 2 16 10 2 12 Java 30
    4. 3 2 6 12 18 C 1
    5. 4 5 8 18 14 C++ 1
    6. 5 20 12 8 6 C# 30
    7. 9 10 4 8 6 Python 4

    通过设置参数ascending可以设置升序或者降序排序,默认情况下ascending=True,为升序排序。

    设置ascending=False时,为降序排序。

    print("按行索引降序排序:\n", df.sort_index(ascending=False), '\n')

    out:

    1. 按行索引降序排序:
    2. A B C D brand years
    3. 9 10 4 8 6 Python 4
    4. 5 20 12 8 6 C# 30
    5. 4 5 8 18 14 C++ 1
    6. 3 2 6 12 18 C 1
    7. 2 16 10 2 12 Java 30

    按列的名称排序:

    设置参数axis=1实现按列的名称排序:

    print("按列名称排序:\n", df.sort_index(axis=1), '\n')

    out:

    1. 按列名称排序:
    2. A B C D brand years
    3. 9 10 4 8 6 Python 4
    4. 3 2 6 12 18 C 1
    5. 4 5 8 18 14 C++ 1
    6. 5 20 12 8 6 C# 30
    7. 2 16 10 2 12 Java 30

    同样,也可以设置ascending参数:

    print("按列名称排序:\n", df.sort_index(axis=1, ascending=False), '\n')

    out:

    1. 按列名称排序:
    2. years brand D C B A
    3. 9 4 Python 6 8 4 10
    4. 3 1 C 18 12 6 2
    5. 4 1 C++ 14 18 8 5
    6. 5 30 C# 6 8 12 20
    7. 2 30 Java 12 2 10 16

    按数值排序:

    sort_values()是pandas中按数值排序的函数:

    1、按单个列的值排序

    sort_values()中设置单个列的列名,可以对单个列进行排序,通过设置ascending可以设置升序或者降序。

    print("按列名称A排序:\n", df.sort_values('A'), '\n')

    out:

    1. 按列名称排序:
    2. A B C D brand years
    3. 3 2 6 12 18 C 1
    4. 4 5 8 18 14 C++ 1
    5. 9 10 4 8 6 Python 4
    6. 2 16 10 2 12 Java 30
    7. 5 20 12 8 6 C# 30

    设置ascending=False进行降序排序:

    print("按列名称A降序排序:\n", df.sort_values('A', ascending=False), '\n')

    out:

    1. 按列名称A降序排序:
    2. A B C D brand years
    3. 5 20 12 8 6 C# 30
    4. 2 16 10 2 12 Java 30
    5. 9 10 4 8 6 Python 4
    6. 4 5 8 18 14 C++ 1
    7. 3 2 6 12 18 C 1

    按多个列的值排序:

    先按year列的数据进行升序排序,year列相同的再看B列进行升序排序

    print("按多个列排序:\n", df.sort_values(['years', 'B']), '\n')

    out:

    1. 按多个列排序:
    2. A B C D brand years
    3. 3 2 6 12 18 C 1
    4. 4 5 8 18 14 C++ 1
    5. 9 10 4 8 6 Python 4
    6. 2 16 10 2 12 Java 30
    7. 5 20 12 8 6 C# 30

    也可以分别设置列的升序、降序来排序:

    years列为升序,B列为降序。

    print("按多个列排序:\n", df.sort_values(['years', 'B'], ascending=[True, False]), '\n')

    out:

    1. 按多个列排序:
    2. A B C D brand years
    3. 4 5 8 18 14 C++ 1
    4. 3 2 6 12 18 C 1
    5. 9 10 4 8 6 Python 4
    6. 5 20 12 8 6 C# 30
    7. 2 16 10 2 12 Java 30

    inplace使用:

    inplace=True:不创建新的对象,直接对原始对象进行修改;默认是False,即创建新的对象进行修改,原对象不变,和深复制和浅复制有些类似。

    1. df.sort_values('A', inplace=True)
    2. print("按A列排序:\n", df, '\n')

    out:

    1. 按A列排序:
    2. A B C D brand years
    3. 3 2 6 12 18 C 1
    4. 4 5 8 18 14 C++ 1
    5. 9 10 4 8 6 Python 4
    6. 2 16 10 2 12 Java 30
    7. 5 20 12 8 6 C# 30

    缺失值:

    含有nan值的数据排序:

    1. data = {
    2. 'brand':['Python', 'C', 'C++', 'C#', 'Java'],
    3. 'B':[4,6,8,np.nan,10],
    4. 'A':[10,2,5,20,16],
    5. 'D':[6,18,14,6,12],
    6. 'years':[4,1,1,30,30],
    7. 'C':[8,12,18,8,2]
    8. }
    9. index = [9,3,4,5,2]
    10. df = pd.DataFrame(data=data, index = index)
    11. print("df数据:\n", df, '\n')

    out:

    1. df数据:
    2. A B C D brand years
    3. 9 10 4.0 8 6 Python 4
    4. 3 2 6.0 12 18 C 1
    5. 4 5 8.0 18 14 C++ 1
    6. 5 20 NaN 8 6 C# 30
    7. 2 16 10.0 2 12 Java 30

    B列含有nan值,对B列进行排序,缺失值排在最前面:

    print("按B列排序:\n", df.sort_values('B', na_position='first'), '\n')
    1. 按B列排序:
    2. A B C D brand years
    3. 5 20 NaN 8 6 C# 30
    4. 9 10 4.0 8 6 Python 4
    5. 3 2 6.0 12 18 C 1
    6. 4 5 8.0 18 14 C++ 1
    7. 2 16 10.0 2 12 Java 30

    包含缺失值,缺失值排在最后:

    print("按B列排序:\n", df.sort_values('B', na_position='last'), '\n')

    out:

    1. 按B列排序:
    2. A B C D brand years
    3. 9 10 4.0 8 6 Python 4
    4. 3 2 6.0 12 18 C 1
    5. 4 5 8.0 18 14 C++ 1
    6. 2 16 10.0 2 12 Java 30
    7. 5 20 NaN 8 6 C# 30

  • 相关阅读:
    InetAddress.getByName背后发生了什么
    Assembling a Query Engine From Spare Parts
    【小沐学NLP】Python实现聊天机器人(OpenAI,模型概述笔记)
    SQL Server2008 + 优化指标(单机或集群服务器)
    AnatoMask论文汇总
    每日一题(刷题记录)
    qt获取qt.conf 中内容过程
    Navisworks二次开发——图元属性获取
    Linux- 网络编程初探
    [go学习笔记.第九章.map] 1.map的介绍,声明,使用方式,crud操作以及遍历
  • 原文地址:https://blog.csdn.net/xiadeliang1111/article/details/126831607