How to apply “first” and “last” functions to columns while using group by in pandas?

后端 未结 4 596
太阳男子
太阳男子 2020-12-08 06:53

I have a data frame and I would like to group it by a particular column (or, in other words, by values from a particular column). I can do it in the following way: gro

4条回答
  •  一向
    一向 (楼主)
    2020-12-08 07:14

    I'm not sure if this is really the issue, but sum and min are Python built-ins that take some iterables as input, whereas first is a method of pandas Series object, so maybe it's not in your namespace. Moreover it takes something else as an input (the doc says some offset value).

    I guess one way to get around it is to create your own first function, and define it such that it takes a Series object as an input, e.g.:

    def first(Series, offset):
        return Series.first(offset)
    

    or something like that..

提交回复
热议问题