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