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

how to combine rows with a key and keep the highest value Pandas

I have one dataframe with several rows and several columns and I need to keep the values from the other columns a,b,c by combining it into a single row of the corresponding key.

Key, ColA, ColB, Colc
111  0     12    0
111  12    0     0
111  0     0     12
222  12    0     0
222  0     0     12

and the output I want is

key, ColA, ColB, ColC
111  12    12    12
222  12    0     12

Thanks for any help

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 :

Keep the max of each group:

>>> df.groupby('Key', as_index=False).max()

   Key  ColA  ColB  Colc
0  111    12    12    12
1  222    12     0    12
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