how to sort within each group of a dataframe while retaining other column

Question:

I am working with a large dataframe with millions of rows.

Sample data:

import pandas as pd
df = pd.DataFrame({'id' : ['c1','c2','c1','c3','c2','c1','c3'],
                  'it' : ['it1','it2','it1','it5','it3','it7','it'],
                  'score' : [.8,.5,1.1,.65,.89,1.2,.91]})

df
    id  it  score
0   c1  it1 0.8
1   c2  it2 0.5
2   c1  it1 1.1
3   c3  it5 0.65
4   c2  it3 0.89
5   c1  it7 1.2
6   c3  it  0.91

I am sorting the dataframe within each groups using:

df.groupby('id', as_index = False).
    apply(pd.DataFrame.sort_values, 'score', ascending=False)

        id  it  score
0   5   c1  it7 1.2
0   2   c1  it1 1.1
0   0   c1  it1 0.8
1   4   c2  it3 0.89
1   1   c2  it2 0.5
2   6   c3  it  0.91
2   3   c3  it5 0.65

But because of large size of the data, the process is taking a lot of time with apply.
Could someone please let me know how to perform the same operation in a much better time efficient way.

Asked By: Karthik S

||

Answers:

You can use a boolean list to sort id and score in ascending/descending order:

df.sort_values(['id','score'], ascending=[True, False])
Answered By: Tranbi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.