Skip to content Skip to sidebar Skip to footer

With Pandas In Python, How Do I Sort By Two Columns Which Are Created By The Agg Function?

For this sort of data author cat val 0 author1 category2 15 1 author2 category4 9 2 author3 category1 7 3 author4 category1 9 4 author5 category2

Solution 1:

You should use .agg instead .apply if you just want to pass two aggregate functions mean and count to your data. Also, since you've applied two functions on the same column val, it will introduce a multi-level column index. So before sorting on newly created columns mean and count, you need to select its outer level val first.

most_expensive_standalone.groupby('cat').agg(['mean', 'count'])['val'].sort(['mean', 'count']


           mean  count
cat                   
category1     8      2
category4     9      1
category2    13      2

Follow-ups:

# just perform groupby and .agg will give you this
most_expensive_standalone.groupby('cat').agg(['mean', 'count'])

           val      
          mean count
cat                 
category1    8     2
category2   13     2
category4    9     1

Select val column

most_expensive_standalone.groupby('cat').agg(['mean', 'count'])['val']


           mean  count
cat                   
category1     8      2
category2    13      2
category4     9      1

And finally call .sort(['mean', 'count'])

Post a Comment for "With Pandas In Python, How Do I Sort By Two Columns Which Are Created By The Agg Function?"