Skip to content
Advertisement

How to get all last rows at second level in MultiIndex DataFrame whose second level has variable length

I have this dataframe:

JavaScript

enter image description here

And I want to keep all the last second level rows, meaning that:

  • For thread_id==0 I want to keep the row message_id_in_thread==1
  • For thread_id==1 I want to keep the row message_id_in_thread==2
  • For thread_id==2 I want to keep the row message_id_in_thread==1

This can easily be achieved by doing df.iterrows(), but I would like to know if there is any direct indexing method.

I look for something like df.loc[(:, -1)], which selects from all (:) level 1 groups, the last (-1) row of that block/group, but obviously this does not work.

Advertisement

Answer

If need both levels use GroupBy.tail:

JavaScript

If need only first level use GroupBy.last or GroupBy.nth:

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