Pandas2.2 DataFrame
Function application, GroupBy & window
method | describe |
---|---|
(func[, axis, raw, …]) | Used to apply a function along the axis (row or column) of a DataFrame |
(func[, na_action]) | Used to apply a function to each element of a DataFrame |
(func[, na_action]) | Used to apply a function to each element in a DataFrame |
(func, *args, **kwargs) | Methods for implementing chain programming styles |
([func, axis]) | Used to perform data on DataFrameAggregation operation |
()
()
(or()
) method is used to perform data of DataFrameAggregation operation. It can apply one or more aggregate functions along a specified axis (row or column) and is commonly used in statistical summary analysis.
Method signature
(func=None, axis=0)
Parameter description
parameter | type | describe |
---|---|---|
func |
function, str, list or dict | The aggregate function to apply. It can be a function name string (such as'sum' ), function objects (such as ), function list, or specify a dictionary of different functions for each column. |
axis |
{0 or ‘index’, 1 or ‘columns’}, default: 0 | Along which axis is the aggregated:0 Indicates aggregation by column (default),1 Indicates aggregation by row. |
Return value
- if
func
is a single aggregate function, then return aSeries
。 - if
func
is multiple aggregate functions or multiple columns aggregation separately, and a is returnedDataFrame
。
Example
Example 1: Use a single aggregate function (such as 'mean')
import pandas as pd df = ({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) result = ('mean') print(result)
Output:
A 2.0
B 5.0
dtype: float64
Example 2: Use multiple aggregate functions (such as ['min', 'max'])
result = (['min', 'max']) print(result)
Output:
A B
min 1 4
max 3 6
Example 3: Use different aggregation functions for different columns
result = ({ 'A': 'mean', 'B': ['min', 'max'] }) print(result)
Output:
A B
mean 2.0 NaN
min NaN 4.0
max NaN 6.0
Example 4: Aggregate by row (axis=1)
result = ('sum', axis=1) print(result)
Output:
0 5
1 7
2 9
dtype: int64
Summarize
-
agg()
Supports multiple aggregation methods and is flexibly applicable to various statistical summary needs. - Different aggregate functions can be specified for different columns.
- Commonly used in data analysis (and
groupby()
More powerful when used together).
This is the end of this article about the use of the () method. For more related () content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!