How to group dataframe rows into list in pandas groupby

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

    Use any of the following groupby and agg recipes.

    # Setup
    df = pd.DataFrame({
      'a': ['A', 'A', 'B', 'B', 'B', 'C'],
      'b': [1, 2, 5, 5, 4, 6],
      'c': ['x', 'y', 'z', 'x', 'y', 'z']
    })
    df
    
       a  b  c
    0  A  1  x
    1  A  2  y
    2  B  5  z
    3  B  5  x
    4  B  4  y
    5  C  6  z
    

    To aggregate multiple columns as lists, use any of the following:

    df.groupby('a').agg(list)
    df.groupby('a').agg(pd.Series.tolist)
    
               b          c
    a                      
    A     [1, 2]     [x, y]
    B  [5, 5, 4]  [z, x, y]
    C        [6]        [z]
    

    To group-listify a single column only, convert the groupby to a SeriesGroupBy object, then call SeriesGroupBy.agg. Use,

    df.groupby('a').agg({'b': list})  # 4.42 ms 
    df.groupby('a')['b'].agg(list)    # 2.76 ms - faster
    
    a
    A       [1, 2]
    B    [5, 5, 4]
    C          [6]
    Name: b, dtype: object
    

提交回复
热议问题