Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - drop first and last row from within each group

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...