Search code examples
pythonnumpypandasdataframemulti-index

Getting the last element of a level in a multiindex


I have a dataframe in this format:

a   b   x
1   1   31
1   2   1
1   3   42
1   4   423
1   5   42
1   6   3
1   7   44
1   8   65437
1   9   73
2   1   5656
2   2   7
2   3   5
2   4   5
2   5   34

a and b are indexes, x is the value.

I want to get rows 1 9 73 and 2 5 34, in other words, the last row of that level.

I've been messing with .loc, .iloc, and .xs for an hour, but I can't get it to work. How do I do this?


Solution

  • You can use groupby with last:

    print (df.groupby('a', as_index=False).last())
       a  b   x
    0  1  9  73
    1  2  5  34
    

    If a and b are levels of MultiIndex, first call reset_index:

    print (df.reset_index().groupby('a', as_index=False).last())
       a  b   x
    0  1  9  73
    1  2  5  34