You can do this using groupby to group on the column of interest and then apply list to every group:
In [1]: df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]})
df
Out[1]:
a b
0 A 1
1 A 2
2 B 5
3 B 5
4 B 4
5 C 6
In [2]: df.groupby('a')['b'].apply(list)
Out[2]:
a
A [1, 2]
B [5, 5, 4]
C [6]
Name: b, dtype: object
In [3]: df1 = df.groupby('a')['b'].apply(list).reset_index(name='new')
df1
Out[3]:
a new
0 A [1, 2]
1 B [5, 5, 4]
2 C [6]
Answer from EdChum on Stack OverflowTutorialsPoint
tutorialspoint.com › python_pandas › python_pandas_groupby.htm
Python Pandas - GroupBy
Pandas objects can be split into groups based on any of their column values using the groupby() method. Let us now see how the grouping objects can be applied to the Pandas DataFrame using the groupby() method.
Medium
medium.com › @heyamit10 › understanding-groupby-and-aggregate-in-pandas-f45e524538b9
Understanding groupby() and aggregate() in Pandas | by Hey Amit | Medium
March 6, 2025 - That’s exactly what groupby() combined with basic aggregation functions like sum(), mean(), and max() can do. Here’s how you do it: import pandas as pd # Sample data data = { 'Department': ['HR', 'HR', 'IT', 'IT', 'Finance', 'Finance'], 'Employee': ['John', 'Emma', 'Steve', 'Mia', 'Tom', 'Sophia'], 'Salary': [50000, 60000, 75000, 80000, 65000, 70000] } df = pd.DataFrame(data) # Sum of salaries by department print(df.groupby('Department')['Salary'].sum()) # Mean salary by department print(df.groupby('Department')['Salary'].mean()) What you’ll get: Department Finance 135000 HR 110000 IT 155000 Name: Salary, dtype: int64 Department Finance 67500.0 HR 55000.0 IT 77500.0 Name: Salary, dtype: float64 ·
Videos
11:05
Group By and Aggregate Functions in Pandas | Python Pandas Tutorials ...
44:17
The Complete Guide to Python Pandas Groupby - YouTube
05:58
How to use Pandas Groupby Like a Pro! - YouTube
19:03
How to use the Pandas GroupBy function | Pandas tutorial - YouTube
10:27
Mastering Pandas .groupby() in 10 Minutes! | Step-by-Step Python ...
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.groupby.html
pandas.DataFrame.groupby — pandas 3.0.2 documentation
Group DataFrame using a mapper or by a Series of columns. A groupby operation involves some combination of splitting the object, applying a function, and combining the results.
Top answer 1 of 16
794
You can do this using groupby to group on the column of interest and then apply list to every group:
In [1]: df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]})
df
Out[1]:
a b
0 A 1
1 A 2
2 B 5
3 B 5
4 B 4
5 C 6
In [2]: df.groupby('a')['b'].apply(list)
Out[2]:
a
A [1, 2]
B [5, 5, 4]
C [6]
Name: b, dtype: object
In [3]: df1 = df.groupby('a')['b'].apply(list).reset_index(name='new')
df1
Out[3]:
a new
0 A [1, 2]
1 B [5, 5, 4]
2 C [6]
2 of 16
134
A handy way to achieve this would be:
df.groupby('a').agg({'b':lambda x: list(x)})
Look into writing Custom Aggregations: https://www.kaggle.com/akshaysehgal/how-to-group-by-aggregate-using-py
GeeksforGeeks
geeksforgeeks.org › pandas › python-pandas-dataframe-groupby
Pandas dataframe.groupby() Method - GeeksforGeeks
Pandas groupby() function is a powerful tool used to split a DataFrame into groups based on one or more columns, allowing for efficient data analysis and aggregation. It follows a "split-apply-combine" strategy, where data is divided into groups, ...
Published July 11, 2025
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.groupby.html
pandas.DataFrame.groupby — pandas documentation
Group DataFrame using a mapper or by a Series of columns. A groupby operation involves some combination of splitting the object, applying a function, and combining the results.
Pandas
pandas.pydata.org › pandas-docs › stable › user_guide › groupby.html
Group by: split-apply-combine — pandas 3.0.2 documentation
An aggregation is a GroupBy operation that reduces the dimension of the grouping object. The result of an aggregation is, or at least is treated as, a scalar value for each column in a group. For example, producing the sum of each column in a group of values.
Pandas
pandas.pydata.org › docs › user_guide › groupby.html
Group by: split-apply-combine — pandas 3.0.1 documentation
An aggregation is a GroupBy operation that reduces the dimension of the grouping object. The result of an aggregation is, or at least is treated as, a scalar value for each column in a group. For example, producing the sum of each column in a group of values.
W3Schools
w3schools.com › python › pandas › ref_df_groupby.asp
Pandas DataFrame groupby() Method
import pandas as pd data = { 'co2': [95, 90, 99, 104, 105, 94, 99, 104], 'model': ['Citigo', 'Fabia', 'Fiesta', 'Rapid', 'Focus', 'Mondeo', 'Octavia', 'B-Max'], 'car': ['Skoda', 'Skoda', 'Ford', 'Skoda', 'Ford', 'Ford', 'Skoda', 'Ford'] } df = pd.DataFrame(data) print(df.groupby(["car"]).mean()) Try it Yourself » ·
Pandas
pandas.pydata.org › docs › reference › groupby.html
GroupBy — pandas 3.0.1 documentation - PyData |
pandas.api.typing.DataFrameGroupBy and pandas.api.typing.SeriesGroupBy instances are returned by groupby calls pandas.DataFrame.groupby() and pandas.Series.groupby() respectively.
GeeksforGeeks
geeksforgeeks.org › pandas › pandas-groupby
Pandas GroupBy - GeeksforGeeks
The groupby() function in Pandas is important for data analysis as it allows us to group data by one or more categories and then apply different functions to those groups.
Published July 11, 2025
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.groupby.html
pyspark.pandas.DataFrame.groupby — PySpark 4.1.1 documentation
Group DataFrame or Series using one or more columns. A groupby operation involves some combination of splitting the object, applying a function, and combining the results.
DataCamp
datacamp.com › tutorial › pandas-groupby
Pandas GroupBy Explained: Syntax, Examples, and Tips | DataCamp
September 22, 2025 - Pandas takes the original DataFrame and partitions it into smaller DataFrames based on the criteria you provide in the by parameter. Each of these smaller DataFrames contains rows that share the same value for the specified key(s). For our example df, if we group by the 'Team' column ...
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.DataFrameGroupBy.mean.html
pandas.core.groupby.DataFrameGroupBy.mean — pandas 2.3.3 documentation
>>> df = pd.DataFrame({'A': [1, 1, 2, 1, 2], ... 'B': [np.nan, 2, 3, 4, 5], ... 'C': [1, 2, 1, 1, 2]}, columns=['A', 'B', 'C']) Groupby one column and return the mean of the remaining columns in each group.
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.DataFrameGroupBy.first.html
pandas.core.groupby.DataFrameGroupBy.first — pandas 2.3.3 documentation
Apply a function groupby to each row or column of a DataFrame. pandas.core.groupby.DataFrameGroupBy.last · Compute the last non-null entry of each column. pandas.core.groupby.DataFrameGroupBy.nth · Take the nth row from each group. Examples · >>> df = pd.DataFrame(dict(A=[1, 1, 3], B=[None, 5, 6], C=[1, 2, 3], ...