Repeating rows in dataframe need to choose minimum value

Viewed 24

Having a data frame as below. if product 1 and product 2 are repeating simultaneously then need to choose the smallest price.

df

expected output:

output

1 Answers

Does this work for you :

data = {"Product1": ["A", "A", "B", "B", "D","D"], 
        "Product2": ["B", "B", "C", "C", "E","F"],
        "location": ["GOA","Banlore","GOA","Banlore","Delhi","Delhi"],
        "Price":[30,40,20,30,10,15]}

df = pd.DataFrame(data)
df = df.loc[df.groupby(['Product1',"Product2"]).Price.idxmin()]
Related