Lee Jack
Lee Jack

Reputation: 191

DataFrame : Get the top n value of each type

I have a group of data like below

ID  Type    value_1 value_2
1   A   12  89
2   A   13  78
3   A   11  92
4   A   9   79
5   B   15  83
6   B   34  91
7   B   2   87
8   B   3   86
9   B   7   85
10  C   9   83
11  C   3   85
12  C   2   87
13  C   12  88
14  C   11  82

I want to get the top 3 member of each Type according to the value_1 . The only solution occurs to me is that: first , get each Type data into a dataframe and sorted according to the value_1 and get the top 3; Then, merge the result together. But is ther any simple method to solve it ? For easy discuss , I have codes below

#coding:utf-8
import pandas as pd
_data = [
    ["1","A",12,89],
    ["2","A",13,78],
    ["3","A",11,92],
    ["4","A",9,79],
    ["5","B",15,83],
    ["6","B",34,91],
    ["7","B",2,87],
    ["8","B",3,86],
    ["9","B",7,85],
    ["10","C",9,83],
    ["11","C",3,85],
    ["12","C",2,87],
    ["13","C",12,88],
    ["14","C",11,82]
]
head= ["ID","type","value_1","value_2"]
df = pd.DataFrame(_data, columns=head)

Upvotes: 1

Views: 50

Answers (2)

Liuhonwun
Liuhonwun

Reputation: 26

Sure! DataFrame.groupby can split a dataframe into different parts by the group fields and apply function can apply UDF on each group.

df.groupby('type', as_index=False, group_keys=False)\
    .apply(lambda x: x.sort_values('value_1', ascending=False).head(3))

Upvotes: 1

BENY
BENY

Reputation: 323226

Then we using groupby tail with sort_values

newdf=df.sort_values(['type','value_1']).groupby('type').tail(3)
newer
    ID type  value_1  value_2
2    3    A       11       92
0    1    A       12       89
1    2    A       13       78
8    9    B        7       85
4    5    B       15       83
5    6    B       34       91
9   10    C        9       83
13  14    C       11       82
12  13    C       12       88

Upvotes: 2

Related Questions