首页 > 解决方案 > 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.

标签: pythonpandasdataframe

解决方案


使用GroupBy.apply

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

推荐阅读