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命令行click参数用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 首先要扩展自带的auth_user表我就放个图吧,同时setting中应该设置:# 此处重载是为了使UserProfile生效AUTH_US
- 1.建立Recordset对象Dim objMyRstSet objMyRst=Server.CreateObject(&ldquo
- 前言将Selenium程序编写为 .bat 可执行文件,从此一键启动封装好的Selenium程序,省时省力还可以复用,岂不美哉应用场景写好
- np.newaxisnp.newaxis 的功能是增加新的维度,但是要注意 np.newaxis 放的位置不同,产生的矩阵形状也不同。通常按
- Selenium爬虫遇到 数据是以 JSON 字符串的形式包裹在 Script 标签中,假设Script标签下代码如下:<script
- 1. js的数据类型1.1 js引入方式<!DOCTYPE html><html lang="en"&
- Psyco 是严格地在 Python 运行时进行操作的。也就是说,Python 源代码是通过 python 命令编译成字节码的,所用的方式和
- 我们知道现实中的数据通常是杂乱无章的,需要大量的预处理才能使用。Pandas 是应用最广泛的数据分析和处理库之一,它提供了多种对原始数据进行
- str字符串s = '中文' # s: <type 'str'>s是个str对象,中
- 因客户需求,要把数据库里的索引编号做成五位长度的,且能自动累加编号,我只会在SQL中使用Identity自动编号:Create Table
- 爬虫请求头网页获取:通过urlopen来进行获取requset.urlopen(url,data,timeout)第一个参数url即为URL
- 很久以前做的东西,发出来大家玩玩!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
- 利用ASP从远程服务器上接收XML数据<% dim objXML dim objRootElement dim&n
- 文件操作此为本人学习python过程中的笔记,将持续更新,欢迎提问指正。1.txt文件1.文本文件 txt2.二进制文件 图片视频操作流程打
- 1. Python的figure参数主要有:def figure(num=None, # autoincrement if None, el
- 问题背景调试脚本时,遇到一个问题:ImportError: cannot import name 'A' from '
- 最近在跑程序,然后Pycharm就跳出out of memory 的错误提示,可能是由于读取的数据太多导致的,Pycharm有一个默认内存的
- 单线程+多任务异步协程协程在函数(特殊函数)定义的时候,使用async修饰,函数调用后,内部语句不会立即执行,而是会返回一个协程对象任务对象
- 如下所示:L = ['adam', 'Lisa', 'bart', 'Paul