Skip to content
Advertisement

Remove duplicates by columns A, keeping the row with the highest value in column B

I have a dataframe with repeat values in column A. I want to drop duplicates, keeping the row with the highest value in column B.

So this:

JavaScript

Should turn into this:

JavaScript

I’m guessing there’s probably an easy way to do this—maybe as easy as sorting the DataFrame before dropping duplicates—but I don’t know groupby’s internal logic well enough to figure it out. Any suggestions?

Advertisement

Answer

This takes the last. Not the maximum though:

JavaScript

You can do also something like:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement