9

I reword my question. I'm searching solution for the following problem:

I have a dataFrame like:

   Sp   Mt   Value  count
4  MM2  S4   bg     10
5  MM2  S4   dgd    1
6  MM4  S2   rd     2
7  MM4  S2   cb     8
8  MM4  S2   uyi    8

My objective is to get ALL the rows where count equal max in each group e.g. :

MM4  S4   bg     10
MM4  S2   cb     8
MM4  S2   uyi    8

I group by ['Sp','Mt']

Somebody knows how can I do it in pandas or in python?

jojo12
  • 4,853
  • 3
  • 14
  • 7

1 Answers1

19
>>> print d
     Sp  Mt Value  Count
ID                      
4   MM2  S4    bg     10
5   MM2  S4   dgd      1
6   MM4  S2    rd      2
7   MM4  S2    cb      8
8   MM4  S2   uyi      8

>>> d.groupby('Sp').apply(lambda t: t[t.Count==t.Count.max()])
         Sp  Mt Value  Count
Sp  ID                      
MM2 4   MM2  S4    bg     10
MM4 7   MM4  S2    cb      8
    8   MM4  S2   uyi      8
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • 3
    I was just about to post `df.groupby(["Sp", "Mt"]).apply(lambda x: x[x["count"] == x["count"].max()])` to the other question; I think the OP wants a groupby on both. – DSM Mar 29 '13 at 17:23