Pandas Groupby - Sort within groups

Pandas Groupby - Sort within groups

To sort within groups while using the groupby function in pandas, you can use the sort_values function after grouping. This allows you to sort the data within each group by a particular column.

Here's how to do it:

  1. Group by a Column and Sort within Groups by Another Column:

    Suppose you have the following DataFrame:

    import pandas as pd data = { 'Category': ['A', 'B', 'A', 'A', 'B', 'B'], 'Value': [10, 15, 5, 20, 10, 25] } df = pd.DataFrame(data) 

    Now, if you want to group by the 'Category' column and then sort within each group by the 'Value' column:

    result = df.groupby('Category', group_keys=False).apply(lambda x: x.sort_values('Value', ascending=False)) print(result) 

    The output will be:

    Category Value 3 A 20 0 A 10 2 A 5 5 B 25 1 B 15 4 B 10 
  2. Group by Multiple Columns and Sort within Groups by Another Column:

    Let's extend the above example by adding another column:

    data['Subcategory'] = [1, 2, 2, 1, 1, 2] df = pd.DataFrame(data) 

    Now, if you want to group by both 'Category' and 'Subcategory' and then sort within each group by 'Value':

    result = df.groupby(['Category', 'Subcategory'], group_keys=False).apply(lambda x: x.sort_values('Value', ascending=False)) print(result) 

    The output will be:

    Category Subcategory Value 3 A 1 20 0 A 1 10 2 A 2 5 5 B 2 25 1 B 2 15 4 B 1 10 

Using this approach, you can sort within groups based on any criteria you'd like by adjusting the lambda function passed to apply.


More Tags

r-factor karma-jasmine word-frequency eonasdan-datetimepicker matplotlib flutter rust widget apache-httpcomponents onmouseout

More Programming Guides

Other Guides

More Programming Examples