drop first and last row from within each group

后端 未结 2 938
灰色年华
灰色年华 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)
    

    0 讨论(0)
  • 2021-01-14 13:50

    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].

    0 讨论(0)
提交回复
热议问题