pandas 对series和dataframe进行排序的实例

作者:乱世流星01 时间:2023-07-16 04:57:50 

本问主要写根据索引或者值对series和dataframe进行排序的实例讲解

代码:


#coding=utf-8
import pandas as pd
import numpy as np
#以下实现排序功能。
series=pd.Series([3,4,1,6],index=['b','a','d','c'])
frame=pd.DataFrame([[2,4,1,5],[3,1,4,5],[5,1,4,2]],columns=['b','a','d','c'],index=['one','two','three'])
print frame
print series
print 'series通过索引进行排序:'
print series.sort_index()
print 'series通过值进行排序:'
print series.sort_values()
print 'dataframe根据行索引进行降序排序(排序时默认升序,调节ascending参数):'
print frame.sort_index(ascending=False)
print 'dataframe根据列索引进行排序:'
print frame.sort_index(axis=1)
print 'dataframe根据值进行排序:'
print frame.sort_values(by='a')
print '通过多个索引进行排序:'
print frame.sort_values(by=['a','c'])

实验结果:


 b a d c
one 2 4 1 5
two 3 1 4 5
three 5 1 4 2

b 3
a 4
d 1
c 6
dtype: int64

series通过索引进行排序:


a 4
b 3
c 6
d 1
dtype: int64

series通过值进行排序:


d 1
b 3
a 4
c 6
dtype: int64

dataframe根据行索引进行降序排序(排序时默认升序,调节ascending参数):


 b a d c
two 3 1 4 5
three 5 1 4 2
one 2 4 1 5

dataframe根据列索引进行排序:


 a b c d
one 4 2 5 1
two 1 3 5 4
three 1 5 2 4

dataframe根据值进行排序:


 b a d c
two 3 1 4 5
three 5 1 4 2
one 2 4 1 5

通过两个索引进行排序:


 b a d c
three 5 1 4 2
two 3 1 4 5
one 2 4 1 5
[Finished in 1.0s]

来源:https://blog.csdn.net/u014662865/article/details/59058039

标签:series,dataframe,排序
0
投稿

猜你喜欢

  • SQL Server把某个字段的数据用一条语句转换成字符串

    2024-01-13 16:10:12
  • 好的Python培训机构应该具备哪些条件

    2022-06-22 14:52:57
  • 提升网站可用性的3个忠告

    2008-01-31 13:48:00
  • 解决Python字典查找报Keyerror的问题

    2021-05-07 00:53:55
  • mysql 5.7.17 安装配置方法图文教程(CentOS7)

    2024-01-19 01:09:22
  • 执行python脚本并传入json数据格式参数方式

    2021-12-09 21:22:14
  • python 如何引入协程和原理分析

    2022-09-06 11:26:07
  • [CSS+JS]同一页面可以重复使用的选项卡

    2009-02-12 12:53:00
  • 设计工作者必须了解的常识

    2008-04-06 13:56:00
  • 使用pandas实现连续数据的离散化处理方式(分箱操作)

    2023-08-28 21:45:51
  • Pytorch可视化的几种实现方法

    2023-06-11 17:44:57
  • Python实现双人五子棋对局

    2022-12-26 04:41:39
  • sqlserver通用的删除服务器上的所有相同后缀的临时表

    2012-06-06 20:07:34
  • linux安装Python3.4.2的操作方法

    2022-06-17 19:19:15
  • 解决mysql 1040错误Too many connections的方法

    2024-01-24 06:56:58
  • 带例子详解Sql中Union和Union ALL的区别

    2024-01-23 01:45:00
  • Python实现从PPT中导出高分辨率图片

    2023-01-03 16:29:08
  • SQL Server如何设置用户只能访问特定数据库和访问特定表或视图

    2024-01-21 07:19:58
  • 如何修改Linux的下MySQL 5.0的默认连接数

    2012-01-29 18:07:04
  • JS实现css边框样式设置工具

    2008-05-25 16:22:00
  • asp之家 网络编程 m.aspxhome.com