Skip to content Skip to sidebar Skip to footer

Display All Group By Columns

''' groupby row, concat list ''' d = {'col1': [33, 33, 33, 34, 34, 34], 'col2': ['hello', 'hello1', 'hello2', 'hello3', 'hello4', 'hello5'], 'col3': [1, 2, 3, 4, 5, 6]} df = p

Solution 1:

IIUC you can use groupby.agg

df1 = df.groupby('col1', as_index=False).agg(list)

print (df1)

   col1                  col2           col3
0   33  [hello, hello1, hello2]     [1, 2, 3]
1   34  [hello3, hello4, hello5]    [4, 5, 6]

Solution 2:

You can use agg with a lambda function to list both your columns.

dfQ = df.groupby('col1').agg(lambda x: list(x)).reset_index()

Post a Comment for "Display All Group By Columns"