Acquire the largest 7 groups after groupby base on index value

Viewed 31

The following dataframe was created by using df.groupby(['Name','Total']) I sorted this by the 'Total' with code .sort_index(level=1) the last step I need to do is to take of the largerst 7 groups base on the value of 'Total'?

Is there any way I can do it without reseting the index?

I try .get_group but not sure how to get more than one,thanks.

Name Total Model Inv
A 99999 145.0 42396
152.5 17800
147.0 15629
157.5 11667
162.5 4881
B 88888 12.5 3892
13.0 21716
12.5 15255
14.0 14013
13.5 2093

. . .

Name Total Model Inv
C 22222 14.5 7892
15.0 51716
12.5 75255
14.0 34013
13.5 1093
D 11111 145.0 42396
152.5 17800
147.0 15629
157.5 11667
162.5 4881
1 Answers

Before(!) using df.groupby() you might consider the following code to achieve your goal:

Totals = df.Total.unique()
Totals.sort()
df.loc[df.Total.isin(Totals[-7:]),:]
Related