Pandas groupby.agg() Function
groupby.agg()It is one of the most powerful aggregation functions in Pandas, allowing you to apply multiple different aggregation functions to grouped data at the same time.
andsum()、mean()Unlike single aggregation functions such as sum, mean, etc.,agg()it can calculate multiple statistical indicators at once, such as calculating the sum, mean, maximum, minimum, etc. of each group simultaneously.
In data analysis, it is often necessary to perform multiple statistical analyses on data at the same time,agg()and this function can make the process concise and efficient.
Basic Syntax and Parameters
agg()It is a member function of the GroupBy object; you need to first usegroupby()groupby() to group the data, and then call it.
Syntax Format
GroupBy.agg(func=None, axis=0, *args, engine=None, engine_kwargs=None, **kwargs)
Parameter Description
| Parameter | Type | Description | Default Value |
|---|---|---|---|
| func | str, list, dict, or callable | Aggregation function. It can be a single function name, a list of functions, or a dictionary specifying different functions for each column. | None |
| axis | int | The axis direction for applying aggregation; 0 means by row (grouping dimension), and 1 means by column. | 0 |
| args | tuple | Additional positional arguments passed to the aggregation function. | () |
| engine | str | Specifies the computation engine, which can be 'cython' or 'numba'. None means Pandas will choose automatically. | None |
| engine_kwargs | dict | A dictionary of additional parameters passed to the underlying engine. | None |
Return Value
- Return Type:
SeriesorDataFrame - Description: Returns the result after grouped aggregation; the result structure depends on the func parameter and the grouping method.
Example
Let us thoroughly master, through a series of examples from simple to complex,groupby.agg()the usage of agg().
Example 1: Using Built-in Aggregation Functions
The simplest way is to directly use the built-in Pandas aggregation function names, such as 'sum', 'mean', 'max', 'min', 'count', etc.
Example
# Create a sales data DataFrame
data = {
'Region': ['North China', 'East China', 'South China', 'North China', 'East China', 'South China', 'North China', 'East China'],
'Product': ['A', 'B', 'C', 'B', 'A', 'C', 'A', 'B'],
'Sales': [1000, 2000, 1500, 1800, 2200, 1600, 1200, 2100],
'Quantity': [10, 20, 15, 18, 22, 16, 12, 21]
}
df = pd.DataFrame(data)
print(Original sales data:)
print(df)
print()
# Group by 'Region' and apply multiple aggregation functions to the 'Sales' column
# Use a list of strings to specify multiple aggregation functions
result = df.groupby('Region')['Sales'].agg(['sum', 'mean', 'max', 'min', 'count'])
print(Summary statistics of sales by region:)
print(result)
print()
# Aggregate both the 'Sales' and 'Quantity' columns
result_all = df.groupby('Region').agg({
'Sales': ['sum', 'mean'],
'Quantity': ['sum', 'mean']
})
print(Comprehensive statistics of sales and quantity by region:)
print(result_all)
Output:
原始销售数据:
地区 产品 销售额 数量
0 华北 A 1000 10
1 华东 B 2000 20
2 华南 C 1500 15
3 华北 B 1800 18
4 华东 A 2200 22
5 华南 C 1600 16
6 华北 A 1200 12
7 华东 B 2100 21
每个地区销售额的汇总统计:
sum mean max min count
地区
华东 7100 1775.0 2200 2000 4
华南 3100 1550.0 1600 1500 2
华北 4000 1333.333333 1800 1000 3
各地区销售和数量的综合统计:
销售额 数量
sum mean sum mean
地区
华东 7100 1775.0 83 20.75
华南 3100 1550.0 31 15.50
华北 4000 1333.333333 40 13.33
Code explanation:
['sum', 'mean', 'max', 'min', 'count']Using a list allows you to specify multiple aggregation functions at the same time.- The returned result is a DataFrame, with column names composed of the function names and the original column names (multi-level column index).
- Using a dictionary allows you to specify different aggregation functions for different columns.
Example 2: Using Custom Aggregation Functions
In addition to built-in functions,agg()it also supports custom functions, which greatly expands its flexibility.
Example
import numpy as np
# Create student grade data
data = {
'Class': ['A', 'A', 'A', 'B', 'B', 'B', 'B', 'A'],
'Name': ['Zhang San', 'Li Si', 'Wang Wu', 'Zhao Liu', 'Sun Qi', 'Zhou Ba', 'Wu Jiu', 'Zheng Shi'],
'Chinese': [85, 92, 78, 88, 95, 82, 90, 87],
'Math': [90, 85, 92, 78, 88, 91, 85, 89]
}
df = pd.DataFrame(data)
print(Student grade data:)
print(df)
print()
# Define custom aggregation functions
def range_func(x):
"""Calculate the difference between the maximum and minimum values (range)"""
return x.max() - x.min()
def coefficient_of_variation(x):
"""Calculate the coefficient of variation (standard deviation / mean)"""
return x.std() / x.mean() * 100
# Use custom functions for aggregation
# You can use the function name (string) or directly pass in the function object
result = df.groupby('Class').agg({
'Chinese': ['sum', 'mean', range_func], # For Chinese: sum, mean, range
'Math': ['sum', 'mean', coefficient_of_variation] # For Math: sum, mean, coefficient of variation
})
print(Custom statistics for each class:)
print(result)
print()
# You can also directly use custom functions in a list
result2 = df.groupby('Class')['Math'].agg(['mean', range_func, 'std'])
print(Statistics of math scores for each class (including custom range and standard deviation):)
print(result2)
Output:
学生成绩数据:
班级 姓名 语文 数学
0 A 张三 85 90
1 A 李四 92 85
2 A 王五 78 92
3 B 赵六 88 78
4 B 孙七 95 88
5 B 周八 82 91
6 B 吴九 90 85
7 A 郑十 87 89
各班级的自定义统计:
语文 数学
sum mean range_func sum mean coefficient_of_variation
班级
A 342 85.5 14.0 425 88.25 5.019099
B 355 88.75 12.0 342 85.5 7.030
各班级数学成绩的统计(包含自定义极差和统计):
班级
A 88.25 5.0 2.692582
B 85.50 13.0 6.0
Code explanation:
- A custom function receives a Series as its parameter and returns a scalar value.
- Function names can be passed as strings (e.g.,
'sum'), or you can directly pass the function object. - Using custom functions allows you to implement arbitrarily complex aggregation logic.
Example 3: Using String Aliases and Lambda Functions
agg()It supports multiple ways of specifying functions, including lambda expressions and built-in string aliases.
Example
# Create employee salary data
data = {
'Department': ['Sales', 'Sales', 'Technology', 'Technology', 'Administration', 'Administration', 'Sales', 'Technology'],
'Position': ['Specialist', 'Manager', 'Specialist', 'Manager', 'Specialist', 'Manager', 'Specialist', 'Manager'],
'Salary': [5000, 8000, 7000, 12000, 4500, 9000, 5500, 11000],
'Years of Service': [2, 5, 3, 8, 1, 6, 2, 7]
}
df = pd.DataFrame(data)
print(Employee salary data:)
print(df)
print()
# Use lambda functions for flexible custom calculations
# Calculate the difference between the median and mean salary for each department
result = df.groupby('Department').agg({
'Salary': [
('Average Salary', 'mean'), # Give aliases to the aggregation results
('Max Salary', 'max'),
('Min Salary', 'min'),
('Salary Range', lambda x: x.max() - x.min())
],
'Years of Service': [
('Average Years of Service', 'mean'),
('Max Years of Service', 'max'),
('Min Years of Service', 'min')
]
})
print(Comprehensive statistics of salary and years of service for each department:)
print(result)
print()
# Use the shorthand form of agg
result2 = df.groupby('Department')['Salary'].agg(
Average='mean',
Sum='sum',
Count='count'
)
print(Aggregation result using named parameters:)
print(result2)
Output:
员工工资数据:
部门 岗位 工资 工龄
0 销售 专员 5000 2
1 销售 经理 8000 5
2 技术 专员 7000 3
3 技术 经理 12000 8
4 行政 专员 4500 1
5 行政 经理 9000 6
6 销售 专员 5500 2
7 技术 经理 11000 7
各部门工资和工龄的综合统计:
工资 工龄
平均工资 最高工资 最低工资 工资极差 平均工龄 最高工龄 最低工龄
部门
技术 10000.0 12000 7000 5000 6.0 8 3
行政 6750.0 9000 4500 4500 3.5 6 1
销售 6166.666667 8000 5000 3000 3.0 5 1
使用命名参数形式的聚合结果:
平均 总和 人数
部门
技术 10000.0 30000 3
行政 6750.0 13500 2
销售 6166.666667 18500 3
Code explanation:
- Using a tuple
('Alias', 'Function Name')you can specify a custom name for the aggregation result column. - Lambda expressions can be directly used in
agg()to implement simple custom logic. - The named parameter form (keyword arguments) allows you to specify names for the result columns more intuitively.
Example 4: Grouping by Multiple Columns and Using agg
agg()It can also be used together with multi-column grouping to handle more complex data analysis needs.
Example
# Create sales data
data = {
'Region': ['North China', 'East China', 'South China', 'North China', 'East China', 'South China', 'North China', 'East China', 'South China'],
'Product': ['A', 'B', 'C', 'B', 'A', 'C', 'A', 'B', 'C'],
'Sales': [1000, 2000, 1500, 1800, 2200, 1600, 1200, 2100, 1700],
'profit': [200, 400, 300, 360, 440, 320, 240, 420, 340]
}
df = pd.DataFrame(data)
print("Sales data:")
print(df)
print()
# Group by region and product, perform multiple aggregations on sales and profit
result = df.groupby(['Region', 'Product']).agg({
'Sales': ['sum', 'mean', 'count'],
'Profit': ['sum', 'mean']
})
print("Aggregation results grouped by region and product:")
print(result)
print()
# Use reset_index to convert to a regular DataFrame format
result_flat = df.groupby(['Region', 'Product'], as_index=False).agg({
'Sales': ['sum', 'mean'],
'Profit': ['sum', 'mean']
})
# Flatten the MultiIndex columns
result_flat.columns = ['_'.join(col).strip('_') for col in result_flat.columns.values]
print("Flattened result:")
print(result_flat)
Output:
销售数据:
地区 产品 销售额 利润
0 华北 A 1000 200
1 华东 B 2000 400
2 华南 C 1500 300
3 华北 B 1800 360
4 华东 A 2200 440
5 华南 C 1600 320
6 华北 A 1200 240
7 华东 B 2100 420
9 华南 C 1700 340
按地区和产品分组后的聚合结果:
销售额 利润
sum mean count sum mean
地区 产品
华东 A 2200 2200.0 1 440 440.0
B 4100 2050.0 2 4100 4100.0
华南 C 3100 1550.0 2 3100 1550.0
华北 A 2200 1100.0 2 440 220.0
B 1800 1800.0 1 360 360.0
展平后的结果:
地区 产品 销售额_sum 销售额_mean 利润_sum 利润_mean
0 华东 A 2200 2200.0 440 440.0
1 华东 B 4100 2050.0 820 410.0
2 华南 C 3100 1550.0 660 330.0
3 华北 A 2200 1100.0 440 220.0
4 华北 B 1800 1800.0 360 360.0
Code analysis:
- Grouping by multiple columns produces a MultiIndex.
- Use
as_index=Falsecan keep the grouping columns as ordinary columns. - By
columnsrenaming can flatten the multi-level column index into a single level.
Note:
agg()It is one of the most commonly used functions in grouping aggregation. It can calculate multiple statistical indicators at once and also supports custom functions, making it highly flexible. It is recommended to master its various usages in real projects.
Other Extensions