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

Is pandas groupby() function always produce a DataFrame with the same order respect to the column we group by?

Given pd.DataFrame X with column id, is the call to groupby() always return a DataFrame with the same order with respect to the column id?

X.groupby('id')[col_name].first()

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

>Solution :

No, by default, groupby sorts the rows on the grouping key(s).

You can disable this behavior by passing sort=False:

X.groupby('id', sort=False)[col_name].first()

Example:

df = pd.DataFrame({'id': [3, 1, 2, 1, 2], 'col_name': list('abcde')})

   id col_name
0   3        a
1   1        b
2   2        c
3   1        d
4   2        e

df.groupby('id')['col_name'].first()

id
1    b
2    c
3    a
Name: col_name, dtype: object

df.groupby('id', sort=False)['col_name'].first()

id
3    a
1    b
2    c
Name: col_name, dtype: object
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