drop first and last row from within each group

后端 未结 2 955
灰色年华
灰色年华 2021-01-14 13:22

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

2条回答
  •  日久生厌
    2021-01-14 13:35

    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)
    

提交回复
热议问题