pandas python 按模式排序 [英] pandas python sorting according to a pattern

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

问题描述

我有一个由 5 列组成的 Pandas 数据框.第二列的数字 1 到 500 重复了 5 次.作为一个较短的例子,第二列是这样的 (1,4,2,4,3,1,1,2,4,3,2,1,4,3,2,3) 我想把它排序成这样 (1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4).我用来排序的代码是 df=res.sort([2],ascending=True) 但这段代码对它进行排序 (1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4).

I have a pandas data frame that consists of 5 columns. The second column has the numbers 1 to 500 repeated 5 times. As a shorter example the second column is something like this (1,4,2,4,3,1,1,2,4,3,2,1,4,3,2,3) and I want to sort it to look like this (1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4). The code i am using to sort is df=res.sort([2],ascending=True) but this code sorts it (1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4).

任何帮助将不胜感激.谢谢

Any help will be much appreciated. Thanks

推荐答案

这个怎么样:排序 cumcount 然后是值本身:

How's about this: sort by the cumcount and then the value itself:

In [11]: df = pd.DataFrame({"s": [1,4,2,4,3,1,1,2,4,3,2,1,4,3,2,3]})

In [12]: df.groupby("s").cumcount()
Out[12]:
0     0
1     0
2     0
3     1
4     0
5     1
6     2
7     1
8     2
9     1
10    2
11    3
12    3
13    2
14    3
15    3
dtype: int64

In [13]: df["s_cumcounts"] = df.groupby("s").cumcount()

In [14]: df.sort_values(["s_cumcounts", "s"])
Out[14]:
    s  s_cumcounts
0   1            0
2   2            0
4   3            0
1   4            0
5   1            1
7   2            1
9   3            1
3   4            1
6   1            2
10  2            2
13  3            2
8   4            2
11  1            3
14  2            3
15  3            3
12  4            3

In [15]: df = df.sort_values(["s_cumcounts", "s"])

In [16]: del df["s_cumcounts"]

这篇关于pandas python 按模式排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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