相关文章推荐
绅士的创口贴  ·  震惊! ...·  1 月前    · 
被表白的橙子  ·  使用Python ...·  1 月前    · 
八块腹肌的柑橘  ·  True Blue Reuse and ...·  3 周前    · 
文质彬彬的冲锋衣  ·  FFmpeg中的时间戳详解_ffmpeg ...·  1 年前    · 
愉快的柿子  ·  [Dev] Soul of the ...·  2 年前    · 
一身肌肉的拖把  ·  ts定义数组中包含多个对象或者或者这种情况在 ...·  2 年前    · 
坚强的领结  ·  8位北舞走出的女星,北舞老师:我培养的人才, ...·  2 年前    · 
Code  ›  Pandas|排序,分组,组内排序开发者社区
dataframe 社区功能 排序算法 pandas
https://cloud.tencent.com/developer/article/1082189
爱看球的围巾
2 年前
作者头像
double
0 篇文章

Pandas|排序,分组,组内排序

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 算法channel > Pandas|排序,分组,组内排序

Pandas|排序,分组,组内排序

作者头像
double
发布 于 2018-04-02 17:18:02
6.5K 0
发布 于 2018-04-02 17:18:02
举报

01

Pandas的基本排序

Pandas的主要数据结构有2个:DataFrame,Series,针对这两个类型的排序Demo如下:

#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)

#series的排序API

print('series通过索引进行排序:')

print(series.sort_index())

print('series通过值进行排序:')

print(series.sort_values())

#dataframe的排序API

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']))

02

输出

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
 
推荐文章
绅士的创口贴  ·  震惊! 居然可以用python在短短几秒内处理几十亿数据!_vaex
1 月前
被表白的橙子  ·  使用Python Pandas处理亿级数据开发者社区
1 月前
八块腹肌的柑橘  ·  True Blue Reuse and Repair | COSAS | Division of Student Affairs | USU
3 周前
文质彬彬的冲锋衣  ·  FFmpeg中的时间戳详解_ffmpeg 保存文件 时间-CSDN博客
1 年前
愉快的柿子  ·  [Dev] Soul of the Ultimate nation(SUN) 1601 Source | Page 2 | RaGEZONE - MMO Development Forums
2 年前
一身肌肉的拖把  ·  ts定义数组中包含多个对象或者或者这种情况在vue3中怎么写_青阳zi的博客-CSDN博客
2 年前
坚强的领结  ·  8位北舞走出的女星,北舞老师:我培养的人才,都被拐去当演员了|金晨|王艳|芭蕾舞|民族舞_网易订阅
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号