I'm sorry that I cannot think of a title.
I have a DF like this:
item_cat1 | item_id | item_count |
---|---|---|
A | 1 | 2 |
B | 2 | 5 |
A | 3 | 4 |
C | 4 | 2 |
I want to get the item that is the most popular among the item category item_cat1
. This is the desired output:
item_cat1 | item_id | item_count |
---|---|---|
A | 3 | 4 |
B | 2 | 5 |
C | 4 | 2 |
Explanation: in item_cat1 A, item_id 3
is sold the most (4) compares to item_id 1
(2).
I tried train.groupby(["item_cat1", "item_id"])["item_count"].sum()
, but I don't know how to choose only the max value.
P/S: what I want is the item_id
, not the item_count
. This answer does not help me: Get the row(s) which have the max value in groups using groupby