Anwaar Khalid
Anwaar Khalid

Reputation: 23

How to access pandas groupby groups in decreasing order of group count in a for loop?

I used pandas groupby to group my data using multiple columns. Now, I want to access the groups in a for loop in decreasing order of group-count?

groups.size().sort_values(ascending = False).head(10) show me the groups in decreasing order of group-count but I want to access each group as a data frame ( like get_group() returns) in a for loop? How do I do that?

This is how the groups look

Upvotes: 0

Views: 271

Answers (1)

mozway
mozway

Reputation: 260380

As hinted in your question, use get_group:

df = pd.DataFrame({'col': list('ABBCACBBC')})

# assign GroupBy object to variable
g = df.groupby('col', sort=False)

# get desired order of the groups
order = g['col'].size().sort_values(ascending=False).index

for x in order:
    print(f'# group {x}')
    print(g.get_group(x))

Output:

# group B
  col
1   B
2   B
6   B
7   B
# group C
  col
3   C
5   C
8   C
# group A
  col
0   A
4   A

Upvotes: 2

Related Questions