Home > OS >  Pick highest values in rows which have the same label in a column
Pick highest values in rows which have the same label in a column

Time:01-20

I have a table like shown below

SKU Stock Past
ABC 0 45
ABC 20 30
DEF 22 0
DEF 5 67

Basically, I just want to change the value of 'Stock' and 'Past' to just one value and pick the highest, so the result should be:

SKU Stock Past
ABC 20 45
DEF 22 67

Is this possible to be done in Pandas? Please advise, anyone. Thank you very much!

CodePudding user response:

You can groupby "SKU" and use max method to find the maximum value of each column in each group:

out = df.groupby('SKU').max()

Output:

     Stock  Past
SKU             
ABC     20    45
DEF     22    67
  •  Tags:  
  • Related