How can I sort within partitions defined by one column but leave the partitions where they are?

前端 未结 2 1163
闹比i
闹比i 2020-12-07 02:08

Consider the dataframe df

df = pd.DataFrame(dict(
        A=list(\'XXYYXXYY\'),
        B=range(8, 0, -1)
    ))

print(df)

   A  B
0  X  8
1           


        
2条回答
  •  生来不讨喜
    2020-12-07 03:14

    You can use transform to get back your new desired index order, then use reindex to reorder your DataFrame:

    # Use transform to return the new ordered index values.
    new_idx = df.groupby('A')['B'].transform(lambda grp: grp.sort_values().index)
    
    # Reindex.
    df = df.reindex(new_idx.rename(None))
    

    You could combine the two lines above into one long line, if so desired.

    The resulting output:

       A  B
    5  X  3
    4  X  4
    7  Y  1
    6  Y  2
    1  X  7
    0  X  8
    3  Y  5
    2  Y  6
    

    Note that if you don't care about maintaing your old index, you can directly reassign from the transform:

    df['B'] = df.groupby('A')['B'].transform(lambda grp: grp.sort_values())
    

    Which yields:

       A  B
    0  X  3
    1  X  4
    2  Y  1
    3  Y  2
    4  X  7
    5  X  8
    6  Y  5
    7  Y  6
    

提交回复
热议问题