python - Pandas pivot_table : aggfunc parameter value & quotation marks

标签 python pandas parameter-passing pivot-table quotation-marks

这两行代码只是传递的参数值不同。我不清楚的是,为什么在第一种情况下(“计数”)我们需要引号,而在第二种情况下(len)不需要引号。

by_weekday1 = users.pivot_table(index='weekday', aggfunc='count')

by_weekday2 = users.pivot_table(index='weekday', aggfunc=len)

提前致谢!

最佳答案

您只能将 Numpy 或 Pandas 方法(换句话说,Pandas 认为是内置 [for Pandas] 的函数)指定为字符串(在引号中),否则它是一个函数(它也可以是一个 numpy 函数) :

users.pivot_table(index='weekday', aggfunc='sum')

类似于:

users.pivot_table(index='weekday', aggfunc=np.sum)

更新:

这是一个 excerpt from the source files :

def _python_agg_general(self, func, *args, **kwargs):
    func = self._is_builtin_func(func)
    ...

哪里_is_builtin_func() defined as follows :

def _is_builtin_func(self, arg):
    """
    if we define an builtin function for this argument, return it,
    otherwise return the arg
    """
    return SelectionMixin._builtin_table.get(arg, arg)

关于python - Pandas pivot_table : aggfunc parameter value & quotation marks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48252750/

相关文章:

c++ - 将对象传递给采用引用的函数,面试测试

javascript - 将参数传递给 GET-Request

python - 将数据拟合到所有可能的分布并返回最佳拟合

python - 我应该使用什么字段作为密码?

python - 从seaborn kdeplots中提取特征参数

python - 将 pandas 数据框合并到新索引级别下

matlab - 是否可以在不运行 Matlab 脚本的情况下在 Simulink 中设置参数?

python - 1005x132589 像素的图像尺寸过大。每个方向必须小于 2^16

Python-根据相关矩阵生成数字

python - Pandas 加入问题 : columns overlap but no suffix specified