繁体   English   中英

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

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

我必须按分组变量 id 对 pandas df 中的列进行排序。 排序不会改变任何其他变量的顺序,除了它自己的 (sq3)。

我的数据看起来像

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

我想实现

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

我尝试了以下有效的代码,但需要很长时间。 任何改进将不胜感激!

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)

演示

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