简体   繁体   English

Pandas 列在组内排序,忽略其他列

[英]Pandas column sort within a group, ignoring other columns

I have to sort a column within a pandas df by a grouped variable id.我必须按分组变量 id 对 pandas df 中的列进行排序。 The sort will not change the order of any other variable, other than it's own (sq3).排序不会改变任何其他变量的顺序,除了它自己的 (sq3)。

My data looks like我的数据看起来像

index id sq1 sq2 sq3
0   0   0   0   0
1   0   0   1   1
2   0   0   2   2
3   0   0   3   3
4   0   0   5   5
5   0   0   4   4
6   0   0   6   6
7   0   0   7   7
8   0   0   8   8
9   0   0   9   9

And I want to achieve我想实现

index id sq1 sq2 sq3
0   0   0   0   0
1   0   0   1   1
2   0   0   2   2
3   0   0   3   3
4   0   0   5   4
5   0   0   4   5
6   0   0   6   6
7   0   0   7   7
8   0   0   8   8
9   0   0   9   9

I have tried the following code that worked, but takes a very long time.我尝试了以下有效的代码,但需要很长时间。 Any improvement will be greatly appreciated!任何改进将不胜感激!

df_groups = df.groupby(['id','sq1'])

for name,group in df_groups:
df_groups.apply(lambda x: x['sq3'].sort_values(ascending=False).values)

transform

df.groupby(['id','sq1']).sq3.transform(sorted)

Demo演示

df.assign(sq3=df.groupby(['id','sq1']).sq3.transform(sorted))

       id  sq1  sq2  sq3
index                   
0       0    0    0    0
1       0    0    1    1
2       0    0    2    2
3       0    0    3    3
4       0    0    5    4
5       0    0    4    5
6       0    0    6    6
7       0    0    7    7
8       0    0    8    8
9       0    0    9    9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM