Pandas Dataframe列值的排序和排序 [英] Pandas Dataframe ordering and sorting of column values

查看:655
本文介绍了Pandas Dataframe列值的排序和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人知道如何通过以下方式对熊猫数据框进行排序:

I was wondering if someone knows a good way on how to sort a pandas dataframe in the following way:

a)我有以下随机排序的数据,其ID多次出现,标签为0或1:

a) I have the following randomly sorted data with an id that appears multiple times and a label that is either 0 or 1:


id | label
------ | ------ 
1 | 1
1 | 0
1 | 0
2 | 1
2 | 0
2 | 0
3 | 0
3 | 0
3 | 0

我想按升序对标签进行排序,然后再对ID进行升序排序,但不进行分组,因此:

I would like to sort the labels in ascending order and then also sort the id's in ascending order, but not grouped, so like this:


id | label
------ | ------ 
1 | 0
2 | 0
3 | 0
1 | 0
2 | 0
3 | 0
3 | 0
1 | 1
2 | 1

提前谢谢!

推荐答案

首先按ID和标签排序,然后使用cumcount创建代表1,2,3组的索引,然后对索引和标签进行排序.

First sort by id and label, then use cumcount to create an index representing 1,2,3 groups, then sort on index and by labels.

df_out = df.sort_values(by=['id','label'])\
  .set_index(df.groupby('id').cumcount())\
  .sort_index()\
  .sort_values(by='label')

输出:

   id  label
0   1      0
0   2      0
0   3      0
1   1      0
1   2      0
1   3      0
2   3      0
2   1      1
2   2      1

这篇关于Pandas Dataframe列值的排序和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆