3

This is a follow up question to get first and last values in a groupby

How do I drop first and last rows within each group?

I have this df

df = pd.DataFrame(np.arange(20).reshape(10, -1),
                  [['a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd'],
                   ['a', 'a', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']],
                  ['X', 'Y'])

df

I intentionally made the second row have the same index value as the first row. I won't have control over the uniqueness of the index.

      X   Y
a a   0   1
  a   2   3
  c   4   5
  d   6   7
b e   8   9
  f  10  11
  g  12  13
c h  14  15
  i  16  17
d j  18  19

I want this

        X   Y
a b   2.0   3
  c   4.0   5
b f  10.0  11

Because both groups at level 0 equal to 'c' and 'd' have less than 3 rows, all rows should be dropped.

Community
  • 1
  • 1
Brian
  • 1,555
  • 3
  • 16
  • 23

2 Answers2

5

I'd apply a similar technique to what I did for the other question:

def first_last(df):
    return df.ix[1:-1]

df.groupby(level=0, group_keys=False).apply(first_last)

enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624
2

Note: in pandas version 0.20.0 and above, ix is deprecated and the use of iloc is encouraged instead.

So the df.ix[1:-1] should be replaced by df.iloc[1:-1].

Radek Lonka
  • 107
  • 3