📅  最后修改于: 2023-12-03 15:18:13.578000             🧑  作者: Mango
Pandas DataFrame.aggregate() is a method used for aggregating data. It allows us to compute a set of summary statistics for the entire DataFrame or a single column.
DataFrame.aggregate(func=None, axis=0, *args, **kwargs)
Consider a DataFrame df
with four columns A
, B
, C
, and D
.
import pandas as pd
data = {'A': [1, 1, 2, 2],
'B': [3, 4, 5, 6],
'C': [7, 8, 9, 10],
'D': [11, 12, 13, 14]}
df = pd.DataFrame(data)
We can compute the average of all the columns using the following command:
df.aggregate('mean')
This will return the average of all the columns:
A 1.5
B 4.5
C 8.5
D 12.5
dtype: float64
Similarly, we can compute multiple summary statistics using a list of functions:
df.aggregate(['sum', 'min', 'max'])
This will return the sum, minimum, and maximum values of all the columns:
A B C D
sum 6 18 34 50
min 1 3 7 11
max 2 6 10 14
We can also apply different functions to different columns using a dictionary:
df.aggregate({'A': 'sum', 'B': 'mean', 'C': 'max', 'D': 'min'})
This will return the sum of column A, the mean of column B, the maximum value of column C, and the minimum value of column D:
A 6
B 4.5
C 10.0
D 11.0
dtype: float64
Pandas DataFrame.aggregate() is a useful method for computing summary statistics for a DataFrame or a single column. We can compute a variety of summary statistics by providing a list of functions, and we can apply different functions to different columns using a dictionary.