2

Say I have the following dataframe:

> df
                   C         D  E
A    B                           
bar  one   -1.350006  0.260339  2
     three -0.236451 -0.056614  0
flux six   -0.515571 -0.155078  1
     three -0.365032  1.055669  2
foo  five   1.145811  1.514191  0
     one   -0.108427 -0.643262  2
     two    0.286897  0.405798  2
     two   -1.452286  1.149264  2

If I do:

df.groupby(level='A').apply(lambda x: x[-1])

I get:

KeyError: -1

Why? I thought I could pass any function to apply on a group.

What I want is the following (get the last entry in every group):

> df
                   C         D  E
A    B                           
bar  one   -0.236451 -0.056614  2
     three -0.236451 -0.056614  2
flux six   -0.365032  1.055669  2
     three -0.365032  1.055669  2
foo  five  -1.452286  1.149264  2
     one   -1.452286  1.149264  2
     two   -1.452286  1.149264  2
     two   -1.452286  1.149264  2
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564

0 Answers0