前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python数据分析扩展库pandas的DataFrame排序方法小结

Python数据分析扩展库pandas的DataFrame排序方法小结

作者头像
Python小屋屋主
发布2018-04-16 15:33:52
7190
发布2018-04-16 15:33:52
举报
文章被收录于专栏:Python小屋Python小屋

>>> import numpy as np >>> import pandas as pd

# 创建DataFrame >>> df = pd.DataFrame(data=[np.random.randint(1,10,4) for i in range(5)],\ index=range(5), columns=list('ABCD')) >>> df A B C D 0 3 3 1 4 1 7 9 1 4 2 1 2 6 2 3 1 9 5 7 4 6 9 2 5

# 对index进行排序 >>> df.sort_index(axis=0, ascending=False) A B C D 4 6 9 2 5 3 1 9 5 7 2 1 2 6 2 1 7 9 1 4 0 3 3 1 4

# 对columns进行排序 >>> df.sort_index(axis=1, ascending=False) D C B A 0 4 1 3 3 1 4 1 9 7 2 2 6 2 1 3 7 5 9 1 4 5 2 9 6

# 按单列进行排序 >>> df.sort_values('A') A B C D 2 1 2 6 2 3 1 9 5 7 0 3 3 1 4 4 6 9 2 5 1 7 9 1 4 >>> df.sort_values('B') A B C D 2 1 2 6 2 0 3 3 1 4 1 7 9 1 4 3 1 9 5 7 4 6 9 2 5

# 按多列进行排序

>>> df.sort_values(['A','B']) A B C D 2 1 2 6 2 3 1 9 5 7 0 3 3 1 4 4 6 9 2 5 1 7 9 1 4 >>> df.sort_values(['B','A']) A B C D 2 1 2 6 2 0 3 3 1 4 3 1 9 5 7 4 6 9 2 5 1 7 9 1 4 # 降序排序 >>> df.sort_values(['B','A'], ascending=False) A B C D 1 7 9 1 4 4 6 9 2 5 3 1 9 5 7 0 3 3 1 4 2 1 2 6 2

# 随机排序

>>> sampler = np.random.permutation(len(df.index)) >>> sampler array([3, 2, 1, 0, 4]) >>> df.take(sampler) A B C D 3 1 9 5 7 2 1 2 6 2 1 7 9 1 4 0 3 3 1 4 4 6 9 2 5

# 随机排序 >>> sampler = np.random.permutation(len(df.index)) >>> sampler array([0, 1, 4, 2, 3]) >>> df.take(sampler) A B C D 0 3 3 1 4 1 7 9 1 4 4 6 9 2 5 2 1 2 6 2 3 1 9 5 7

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档