Column sorting and filtering for best results, grouping using multiple index 0 index (Series / DataFrame)

I would like to group the top results for each item in the index at level 0. For example, using this data / series block:

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 Aboth Bgrouped and return the top 3 val(decreasing) from each.

+4
source share
3 answers

Perhaps this is what you are looking for

df.groupby(level=0, group_keys=False).apply(lambda x: x.sort_values(ascending=False).head(3))

i0  i1
A   1     0.720324
    0     0.417022
    3     0.302333
B   9     0.538817
    8     0.396767
    7     0.345561
+3
source

Another similar option:

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

enter image description here

+4
source

, nlargest

df.groupby(level=0, group_keys=False).nlargest(3)

i0  i1
A   1     0.720324
    0     0.417022
    3     0.302333
B   9     0.538817
    8     0.396767
    7     0.345561
Name: val, dtype: float64
+3

Source: https://habr.com/ru/post/1664693/


All Articles