Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to apply the aggregation list on every group of pandas DataFrame?
To apply aggregation as a list on every group of a Pandas DataFrame, use the agg() method with list as the aggregation function. This combines all values within each group into a list.
Importing Required Library
First, import Pandas −
import pandas as pd
Creating a Sample DataFrame
Let's create a DataFrame with car data to demonstrate grouping ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'],
"Units": [100, 150, 110, 80, 110, 90]
}
)
print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
Car Units
0 BMW 100
1 Lexus 150
2 Lexus 110
3 Mustang 80
4 Bentley 110
5 Mustang 90
Applying List Aggregation on Groups
Group by 'Car' column and aggregate using list to combine all units for each car brand ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'],
"Units": [100, 150, 110, 80, 110, 90]
}
)
# Group by 'Car' and aggregate as list
grouped_df = dataFrame.groupby('Car').agg(list)
print("DataFrame with list aggregation:")
print(grouped_df)
DataFrame with list aggregation:
Units
Car
BMW [100]
Bentley [110]
Lexus [150, 110]
Mustang [80, 90]
Multiple Column Aggregation
You can also apply list aggregation to multiple columns simultaneously ?
import pandas as pd
# Create DataFrame with additional column
dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'],
"Units": [100, 150, 110, 80, 110, 90],
"Price": [50000, 60000, 65000, 45000, 80000, 47000]
}
)
# Group by 'Car' and aggregate both columns as lists
grouped_df = dataFrame.groupby('Car').agg(list)
print("Multiple column list aggregation:")
print(grouped_df)
Multiple column list aggregation:
Units Price
Car
BMW [100] [50000]
Bentley [110] [80000]
Lexus [150, 110] [60000, 65000]
Mustang [80, 90] [45000, 47000]
Conclusion
Use groupby().agg(list) to combine all values within each group into lists. This is useful for preserving all individual values while organizing data by groups. The result shows each unique group with its corresponding aggregated lists.
Advertisements
