Skip to content
Advertisement

How to get last group in Pandas’ groupBy?

I wish to get the last group of my group by:

JavaScript

but that gives the error:

KeyError: -1

Using get_group is useless as I don’t know the last group’s value (unless there’s a specific way to get that value?). Also I might want to get the last 2 groups, etc

How do I do this?

Advertisement

Answer

You can call last which computes the last values for each group and use iloc to get the row values and access the index group values using the name attribute, there is probably a better way but unable to figure this out yet:

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