How to group dataframe rows into list in pandas groupby

前端 未结 12 2182
日久生厌
日久生厌 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:52

    It is time to use agg instead of apply .

    When

    df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6], 'c': [1,2,5,5,4,6]})
    

    If you want multiple columns stack into list , result in pd.DataFrame

    df.groupby('a')[['b', 'c']].agg(list)
    # or 
    df.groupby('a').agg(list)
    

    If you want single column in list, result in ps.Series

    df.groupby('a')['b'].agg(list)
    #or
    df.groupby('a')['b'].apply(list)
    

    Note, result in pd.DataFrame is about 10x slower than result in ps.Series when you only aggregate single column, use it in multicolumns case .

提交回复
热议问题