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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With