sorting - Python Pandas sort by column, but keep index same -
i created data frame consists of country, deal_category, , some_metric.
it looks
country metric_count channel 0 country1 123472 c1 1 country1 159392 c2 2 country2 14599 c3 3 country2 17382 c4
i indexed according country , channel using command
df2 = df.set_index(["country", "channel"])
this creates following dataframe.
metric_count country channel country1 category1 12347 category2 159392 category3 14599 category4 17382 country2 category1 1234
here's want do. i'd keep structure same , sort according metric counts. in other words, i'd display each country, top 3 channels based on metric count.
for instance, i'd dataframe display each country, top 3 categories ordered descending metric_counts.
country2 top category1 12355555 top category2 159393 top category3 16759
i've tried sorting first, indexing, resulting data frame no longer partitions based on country. tips appreciated. thanks!
after taxing experimentation, able wanted. outline steps below
groupby country
group = df.groupby("country")
at high-level, indicates @ each country differently. our goal determine top 3 metric counts , report corresponding channel. this, apply sort resulting data-frame , return top 3 results. can defining sort function returns top 3 results , use apply function in pandas. indicates panda "i want apply sort function each of our groups , return top 3 results each group".
sort , return top 3
sort_function = lambda x: x.sort("metric_count", ascending = false)[:3] desired_df = group.apply(sort_function)
Comments
Post a Comment