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

Advertisements

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()

>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

Leave a ReplyCancel reply