coding_monkey
coding_monkey

Reputation: 399

Pandas sorting multiple columns

I have the following dataframe

A   B
b   10
b   5
a   25
a   5
c   6
c   2
b   20
a   10
c   4
c   3
b   15

How can I sort it as follows:

A   B
b   20
b   15
b   10
b   5
a   25
a   10
a   5
c   6
c   4
c   3
c   2

Column A is sorted based on the sum of corresponding values in column B in descending order(The sums are b-50, a-40, c-15) .

Upvotes: 1

Views: 477

Answers (1)

Zero
Zero

Reputation: 76917

Create a temporary column _t and sort using sort_values on _t, B

In [269]: (df.assign(_t=df['A'].map(df.groupby('A')['B'].sum()))
            .sort_values(by=['_t', 'B'], ascending=False)
            .drop('_t', 1))
Out[269]:
    A   B
6   b  20
10  b  15
0   b  10
1   b   5
2   a  25
7   a  10
3   a   5
4   c   6
8   c   4
9   c   3
5   c   2

Details

In [270]: df.assign(_t=df['A'].map(df.groupby('A')['B'].sum()))
Out[270]:
    A   B  _t
0   b  10  50
1   b   5  50
2   a  25  40
3   a   5  40
4   c   6  15
5   c   2  15
6   b  20  50
7   a  10  40
8   c   4  15
9   c   3  15
10  b  15  50

Upvotes: 2

Related Questions