pandas多级分组实现排序的方法
作者:飞羽喂马 时间:2022-05-06 14:16:11
pandas有groupby分组函数和sort_values排序函数,但是如何对dataframe分组之后排序呢?
In [70]: df = pd.DataFrame(((random.randint(2012, 2016), random.choice(['tech', 'art', 'office']), '%dk-%dk'%(random.randint(2,10), random.randint(10, 20)), '') for _ in xrange(10000)), columns=['publish_time', 'classf', 'salary', 'title'])
In [71]: df.head()
Out[71]:
publish_time classf salary title
0 2012 art 2k-19k
1 2014 office 5k-17k
2 2013 office 2k-10k
3 2013 art 5k-14k
4 2013 art 2k-14k
In [72]: df.groupby(['publish_time', 'classf', 'salary']).count()['title'].groupby(level=0, group_keys=False).nlargest(10)
Out[72]:
publish_time classf salary
2012 art 7k-13k 18
4k-13k 16
tech 3k-12k 14
art 6k-16k 13
8k-15k 13
office 5k-18k 13
tech 4k-14k 13
来源:https://blog.csdn.net/qq_35318838/article/details/77051109
标签:pandas,分组,排序
0
投稿
猜你喜欢
如何通过python检查文件是否被占用
2023-03-20 12:25:00
python 三边测量定位的实现代码
2023-02-03 08:37:31
python备份文件的脚本
2023-12-14 10:52:02
JS页内查找关键词的高亮显示
2013-07-18 21:13:54
VBScript中变量作用域
2007-11-02 10:03:00
和“登录”有关的事儿
2009-07-10 17:37:00
python 制作python包,封装成可用模块教程
2022-06-02 06:32:25
python实现爬虫统计学校BBS男女比例之数据处理(三)
2022-12-11 18:56:45
通过python实现弹窗广告拦截过程详解
2022-04-12 09:21:31
insert select与select into 的用法使用说明
2012-01-05 18:47:58
在Python中将函数作为另一个函数的参数传入并调用的方法
2023-04-10 07:32:26
如何由Sybase向SQL Server移植数据库
2009-01-20 15:56:00
Python全局变量操作详解
2022-04-18 04:22:00
用python将pdf转化为有声读物
2021-02-02 10:06:46
Python爬虫番外篇之Cookie和Session详解
2022-02-09 18:56:44
pandas将list数据拆分成行或列的实现
2023-12-23 01:58:41
Python爬虫框架Scrapy基本用法入门教程
2021-08-17 19:50:45
解读HTML:大厦的基石
2008-12-01 12:57:00
PyQt5每天必学之单行文本框
2022-09-12 06:29:35
Python编程matplotlib绘图挑钻石seaborn小提琴和箱线图
2022-03-06 20:08:29