Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading