Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting column and filtering for top results, grouping by multiindex level 0 (Series / DataFrame)

Tags:

python

pandas

I would like to group out the top results for each element in the index at level 0. For example with this dataframe / series:

import pandas as pd
import numpy as np
np.random.seed(1)

index = list(zip(['A']*5 + ['B']*5, list(range(10))))
df = pd.Series(np.random.random((10)),
               index=pd.MultiIndex.from_tuples(index, names=['i0', 'i1']),
               name='val')

pd.DataFrame(df)

enter image description here

I would like to keep A and B grouped and return the top 3 val's (descending) from each.

like image 533
AlexG Avatar asked Dec 23 '22 22:12

AlexG


1 Answers

Another similar option here:

(df.sort_values('val', ascending=False).groupby(level=0).head(3)
   .sort_index(level = 0, sort_remaining=False, kind="mergesort"))

enter image description here

like image 186
Psidom Avatar answered Jan 14 '23 05:01

Psidom