df.groupby(["ClientID", "FoodID"])['Quantity'].sum().reset_index().sort_values(
["ClientID", 'Quantity'], ascending=False).drop_duplicates(
["ClientID"]).sort_values('ClientID')
First get a df with contains the total Quantity
for each ClientID
, FoodID
combination. Then sort the df on ClientID
, Quantity
so that highest Quantity
per client appears on the top and finally drop the duplicates per client which will drop all the clients records except the top which happens to be max quantity.
Test case:
np.random.seed(0)
df = pd.DataFrame({
'ClientID' : np.random.randint(1,10, 1000),
'FoodID' : np.random.randint(1,10, 1000),
'Quantity' : np.random.randint(1,10, 1000),
})
df.groupby(["ClientID", "FoodID"])['Quantity'].sum().reset_index().sort_values(
["ClientID", 'Quantity'], ascending=False).drop_duplicates(
["ClientID"]).sort_values('ClientID')
Output:
ClientID FoodID Quantity
3 1 4 97
16 2 8 82
26 3 9 100
35 4 9 98
44 5 9 85
47 6 3 107
54 7 1 94
69 8 7 107
73 9 2 109
CLICK HERE to find out more related problems solutions.