pandas.NamedAgg#

class pandas.NamedAgg(column, aggfunc)[源代码]#

用于对特定列进行聚合的辅助工具,可控制输出列名。

typing.NamedTuple 的子类。

Parameters:
columnHashable

DataFrame 中要应用 aggfunc 的列标签。

aggfuncfunction 或 str

应用于给定列的函数。如果是字符串,则为内置 pandas 函数的名称。

Examples

>>> df = pd.DataFrame({"key": [1, 1, 2], "a": [-1, 0, 1], 1: [10, 11, 12]})
>>> agg_a = pd.NamedAgg(column="a", aggfunc="min")
>>> agg_1 = pd.NamedAgg(column=1, aggfunc=lambda x: np.mean(x))
>>> df.groupby("key").agg(result_a=agg_a, result_1=agg_1)
     result_a  result_1
key
1          -1      10.5
2           1      12.0

Attributes

aggfunc 

字段编号 1 的别名

column 

字段编号 0 的别名

Methods

count (value, /)

返回 value 的出现次数。

index (value[, start, stop])

返回 value 的第一个索引。