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

后端 未结 4 612
太阳男子
太阳男子 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:10

    I would use a custom aggregator as shown below.

    d = pd.DataFrame([[1,"man"], [1, "woman"], [1, "girl"], [2,"man"], [2, "woman"]],columns = 'number family'.split())
    d
    

    Here is the output:

        number family
     0       1    man
     1       1  woman
     2       1   girl
     3       2    man
     4       2  woman
    

    Now the Aggregation taking first and last elements.

    d.groupby(by = "number").agg(firstFamily= ('family', lambda x: list(x)[0]), lastFamily =('family', lambda x: list(x)[-1]))
    

    The output of this aggregation is shown below.

           firstFamily lastFamily
    number                       
    1              man       girl
    2              man      woman
    

    I hope this helps.

提交回复
热议问题