I created a data frame that consists of a Country, deal_category, and some_metric.
It looks like
Country metric_count channel
0 Country1 123472 c1
1 Country1 159392 c2
2 Country2 14599 c3
3 Country2 17382 c4
I indexed according to Country and channel using the command
df2 = df.set_index(["Country", "channel"])
This creates the following dataframe.
metric_count
Country channel
Country1 category1 12347
category2 159392
category3 14599
category4 17382
Country2 category1 1234
Here's what I want to do. I'd like to keep this structure the same and sort according to the metric counts. In other words, I'd like to display for each country, the top 3 channels based on the metric count.
For instance, I'd like a dataframe to display for each country, the top 3 categories ordered by descending metric_counts.
Country2 top category1 12355555
top category2 159393
top category3 16759
I've tried sorting first, then indexing, but the resulting data frame no longer partitions based on country. Any tips would be greatly appreciated. Thanks!