python - How to sort rows in pandas in place for given group?
问题描述
I am playing around and trying to learn pandas. And currently stuck at one point,
Data -
A B C
---------------------------
1 1 1
1 1 2
1 1 3
2 1 3
2 1 1
2 1 2
1 2 2
1 2 1
1 2 3
And My expected output is,
A B C
---------------------------
1 1 1
1 1 2
1 1 3
2 1 1
2 1 2
2 1 3
1 2 1
1 2 2
1 2 3
The orignal data looks much complicated with other columns. This is just simplifies version of that data. So basically what I want is to sort the data for Col C in place for combined unique key of (Col A and B).
Currently what I am trying to do is,
contentIDs = data.B.unique()
for iD in contentIDs:
slots = data[data.B == iD].A.unique()
for s in slots:
slotData = data[(data.A == s) & (data.B == iD)]
sortedData = slotData.sort_values(['A', 'B', 'C'])
#Loop throug data to get to the index of sorted data and then replace unsorted data with sorted data.
I thought there might be better way to do this. So asking here if there is better way before I move ahead with my looping logic. Which looks pretty bad for doing simple thing like this.
Any suggestion or pointers are welcome. Please feel free to comment in case of any confusion.
解决方案
df.groupby(['A','B'],sort=False)['C'].apply(lambda x:x.sort_values()).reset_index(level=['A','B'])
A B C
0 1 1 1
1 1 1 2
2 1 1 3
4 2 1 1
5 2 1 2
3 2 1 3
7 1 2 1
6 1 2 2
8 1 2 3
推荐阅读
- ios - 模拟器黑屏不加载
- skiasharp - Skia:使用 CPU 渲染器优于 GPU 渲染器的任何好处
- powershell - 在其他脚本中使用变量/从脚本中提取变量
- r - 向 geom_col 添加值会破坏整个情节
- typescript - 根据另一个属性值键入 Svelte 组件属性
- java - 数组扩展/缩减
- python-3.x - SQLAlchemy 会话错误消息
- spring-security - 带有 Spring Security 的 Java Struts2 和 Filter Chain 实现
- python - Excel 通过 selenium driver.get url (csv) 加载到 pandas?
- css - 如何在 css3 中使可变颜色变亮?