How to group dataframe rows into list in Pandas Groupby? - GeeksforGeeks

02 Feb, 2021
Suppose you have a pandas DataFrame consisting of 2 columns and we want to group these columns. In this article, we will discuss about the same. First, let;s create the dataframe.
Python3
# importing pandas as pdimport pandas as pd    # Create the data framedf = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',                               'C', 'B', 'D', 'D', 'A'],                   'column2': [5, 10, 15, 20, 25, 30,                              35, 40, 45, 50]})  # Print the dataframedf
Output:
Example #1: We can use groupby() method on column 1 and apply the method to apply a list on every group of pandas DataFrame.
Python3
# importing pandas as pdimport pandas as pd    # Create the data framedf = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',                               'C', 'B', 'D', 'D', 'A'],                   'column2': [5, 10, 15, 20, 25, 30,                              35, 40, 45, 50]})    # Use groupby method and apply# method on the dataframedf = df.groupby('column1')['column2'].apply(list)  # Print the dataframe againdf
Output:
Example #2: 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
# importing pandas as pdimport pandas as pd    # Create the dataframedf = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',                                'C', 'B', 'D', 'D', 'A'],                    'column2': [5, 10, 15, 20, 25, 30,                               35, 40, 45, 50]})  # Use groupby method and agg method # with lambda function on the dataframedf = df.groupby('column1').agg({'column2': lambda x: list(x)})  # Print the dataframe againdf
Output:
Example #3: We can use the groupby() method on column 1 and agg() method to apply the aggregation list, on every group of pandas DataFrame.
Python3
# importing pandas as pdimport pandas as pd    # Create the data framedf = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',                               'C', 'B', 'D', 'D', 'A'],                   'column2': [5, 10, 15, 20, 25, 30,                                35, 40, 45, 50]})  # Use groupby method and agg method # with list as argument on the dataframedf = df.groupby('column1').agg(list)  df
Output:
Example #4: We can use groupby() method on column 1 and agg() method by passing ‘pd.Series.tolist’ as an argument.
Python3
# importing pandas as pdimport pandas as pd    # Create the data framedf = pd.DataFrame({'column1': ['A', 'B', 'C', 'A', 'C',                                'C', 'B', 'D', 'D', 'A'],                    'column2': [5, 10, 15, 20, 25, 30,                               35, 40, 45, 50]})  # Use groupby method and agg method with# pd.Series.tolist as argument on the dataframedf = df.groupby('column1').agg(pd.Series.tolist)  df
Output:
Python pandas-dataFrame
Python pandas-groupby
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python