pandas pivot_table:aggfunc参数值&引号
pandas pivot_table: aggfunc parameter value & quotation marks
这两行代码只是传递的参数值不同。我不清楚的是为什么在第一种情况 ("count") 中我们需要引号,而在第二种情况 (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)
这两行代码只是传递的参数值不同。我不清楚的是为什么在第一种情况 ("count") 中我们需要引号,而在第二种情况 (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)