How to group dataframe rows into list in pandas groupby

前端 未结 12 2134
日久生厌
日久生厌 2020-11-21 04:56

I have a pandas data frame df like:

a b
A 1
A 2
B 5
B 5
B 4
C 6

I want to group by the first column and get second col

12条回答
  •  孤街浪徒
    2020-11-21 05:41

    A handy way to achieve this would be:

    df.groupby('a').agg({'b':lambda x: list(x)})
    

    Look into writing Custom Aggregations: https://www.kaggle.com/akshaysehgal/how-to-group-by-aggregate-using-py

提交回复
热议问题