Skip to content
Advertisement

Filter column for multiple values but only select the last one for one criteria

I have a dataframe similar to this one

JavaScript

I would now like to slice the df based on multiple values from column ‘categorie’ and am currently using

JavaScript

In addition to that I would like to be able to only get the [-1] row back for categorie ‘a’

JavaScript

instead of

JavaScript

I think the closest would be to think about it as a groupby max value on id and categorie but I am curious if there is a more pythonic way.

Advertisement

Answer

‘a’ and ‘c’ are the only categories in your data, if you just need the latest then drop the duplicates

JavaScript

or

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