How to group dataframe rows into list in Pandas Groupby?
Last Updated :
15 Sep, 2022
Suppose you have a Pandas DataFrame consisting of 2 columns and we want to group these columns. In this article, we will discuss the same.
Creating Dataframe to group Dataframe rows into a list
Python3
import pandas as pd
df = pd.DataFrame({ 'column1' : [ 'A' , 'B' , 'C' , 'A' , 'C' ,
'C' , 'B' , 'D' , 'D' , 'A' ],
'column2' : [ 5 , 10 , 15 , 20 , 25 , 30 ,
35 , 40 , 45 , 50 ]})
df
|
Output:
Group rows into a list in Pandas using apply()
We can use groupby() method on column 1 and apply the method to apply a list on every group of pandas DataFrame.
Python3
import pandas as pd
df = pd.DataFrame({ 'column1' : [ 'A' , 'B' , 'C' , 'A' , 'C' ,
'C' , 'B' , 'D' , 'D' , 'A' ],
'column2' : [ 5 , 10 , 15 , 20 , 25 , 30 ,
35 , 40 , 45 , 50 ]})
df = df.groupby( 'column1' )[ 'column2' ]. apply ( list )
df
|
Output:
Group rows into a list in Pandas using lambda
We can use groupby() method on column 1 and agg() method to apply aggregation, consisting of the lambda function, on every group of pandas DataFrame.
Python3
import pandas as pd
df = pd.DataFrame({ 'column1' : [ 'A' , 'B' , 'C' , 'A' , 'C' ,
'C' , 'B' , 'D' , 'D' , 'A' ],
'column2' : [ 5 , 10 , 15 , 20 , 25 , 30 ,
35 , 40 , 45 , 50 ]})
df = df.groupby( 'column1' ).agg({ 'column2' : lambda x: list (x)})
df
|
Output:
Group rows into a list in Pandas using agg()
We can use the groupby() method on column1, and agg() method to apply the aggregation list, on every group of pandas DataFrame.
Python3
import pandas as pd
df = pd.DataFrame({ 'column1' : [ 'A' , 'B' , 'C' , 'A' , 'C' ,
'C' , 'B' , 'D' , 'D' , 'A' ],
'column2' : [ 5 , 10 , 15 , 20 , 25 , 30 ,
35 , 40 , 45 , 50 ]})
df = df.groupby( 'column1' ).agg( list )
df
|
Output:
Group rows into a list in Pandas using Pandas tolist
We can use groupby() method on column 1 and agg() method by passing ‘pd.Series.tolist’ as an argument.
Python3
import pandas as pd
df = pd.DataFrame({ 'column1' : [ 'A' , 'B' , 'C' , 'A' , 'C' ,
'C' , 'B' , 'D' , 'D' , 'A' ],
'column2' : [ 5 , 10 , 15 , 20 , 25 , 30 ,
35 , 40 , 45 , 50 ]})
df = df.groupby( 'column1' ).agg(pd.Series.tolist)
df
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...