Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 345
parchment Avatar asked Jun 19 '16 07:06

parchment


1 Answers

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
like image 104
jezrael Avatar answered Oct 28 '22 00:10

jezrael