简体   繁体   中英

How to keep top 3 rows of each group in a pandas data frame?

I have a list that looks like this:

var1     var2    count
 A        abc      4
 A        abc      3
 A        abc      2
 A        abc      1
 A        abc      1
 B        abc      7
 B        abc      5
 B        abc      2
 B        abc      1
 B        abc      1
 C        abc      4
 C        abc      3
 C        abc      2
 C        abc      1
 C        abc      1

 ....

I want to create a new dataframe with top 3 'count' results from each group. It should look like this:

     var1     var2    count
      A        abc      4
      A        abc      3
      A        abc      2
      B        abc      7
      B        abc      5
      B        abc      2
      C        abc      4
      C        abc      3
      C        abc      2
      ....

Is there a convenient way to do this in Python using head()?

Solution with set_index , groupby and SeriesGroupBy.nlargest :

df = df.set_index('var2').groupby("var1")['count'].nlargest(3).reset_index()
print (df)
  var1 var2  count
0    A  abc      4
1    A  abc      3
2    A  abc      2
3    B  abc      7
4    B  abc      5
5    B  abc      2
6    C  abc      4
7    C  abc      3
8    C  abc      2

If the count column has been sorted in descending order, then you can just use groupby.head to take the first three rows from each group:

df.groupby("var1").head(3)

在此输入图像描述

Otherwise, you can group data frame by var1 and use nlargest to retrieve the three rows with top 3 counts:

df.groupby("var1", group_keys=False).apply(lambda g: g.nlargest(3, "count"))

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM