Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

df.sort_values(['id','score'], ascending=[True, False])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading