How to Sort Results within Groups Using GroupBy and Aggregation in Pandas?

DDD
Release: 2024-10-20 17:22:31
Original
309 people have browsed it

How to Sort Results within Groups Using GroupBy and Aggregation in Pandas?

pandas groupby and sorting within groups

Wanting to sort the results of a groupby aggregation is a common task. In this example, we have a DataFrame with two columns, 'job' and 'source', and a 'count' column that we want to group by and sort.

To do this, we can use the groupby() method to group the DataFrame by the 'job' and 'source' columns. We can then use the agg() method to aggregate the 'count' column, in this case using the sum function.

In [168]: df.groupby(['job','source']).agg({'count':sum})

Out[168]:
               count
job    source       
market A           5
       B           3
       C           2
       D           4
       E           1
sales  A           2
       B           4
       C           6
       D           3
       E           7
Copy after login

This will give us a new DataFrame with the grouped results. We can then use the sort_values() method to sort the 'count' column in descending order within each of the groups.

In [34]: df.sort_values(['job','count'],ascending=False).groupby('job').head(3)

Out[35]: 
   count     job source
4      7   sales      E
2      6   sales      C
1      4   sales      B
5      5  market      A
8      4  market      D
6      3  market      B
Copy after login

This will give us a new DataFrame with the top 3 results for each group.

The above is the detailed content of How to Sort Results within Groups Using GroupBy and Aggregation in Pandas?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!