[英]Python/Pandas: Using built-in functions as arguments in user-defined function
Very simple query but can't seem to find appropriate answer.非常简单的查询,但似乎找不到合适的答案。 I want to pass the Pandas method for eg .sum() as an input to my function.
我想将 Pandas 方法传递给例如 .sum() 作为我函数的输入。
def something(dataframe,col_name,func):
return dataframe.col_name.func
something(df,'a',sum())
TypeError: sum expected at least 1 arguments, got 0.类型错误:求和至少有 1 个参数,结果为 0。
Python confuses it with inbuilt function sum() Python 将其与内置函数 sum() 混淆
You an use operator.methodcaller
for this:您为此使用
operator.methodcaller
:
from operator import methodcaller
df = pd.DataFrame({'a': range(11)})
def foo(df, col, method):
return methodcaller(method)(df[col])
res_sum = foo(df, 'a', 'sum') # 55
res_avg = foo(df, 'a', 'mean') # 5.0
The reason for your error is you are trying to pass the result of a called function with no arguments, one that happens to require arguments to work.你的错误的原因是你试图传递一个没有参数的被调用函数的结果,一个恰好需要参数才能工作的函数。
The benefit of passing strings is you rely on tried-and-tested methods built into the Pandas framework, eg pd.Series.sum
, pd.Series.mean
, etc. While you can attempt to use Python built-ins and NumPy functions directly with Pandas series, you may find discrepancies versus what you might expect.传递字符串的好处是您依赖于 Pandas 框架中内置的久经考验的方法,例如
pd.Series.sum
、 pd.Series.mean
等。 虽然您可以尝试直接使用 Python 内置函数和 NumPy 函数使用 Pandas 系列,您可能会发现与您预期的不一致。 Stick with documented Pandas methods where possible.尽可能坚持使用记录在案的 Pandas 方法。
Do not recommend acquiring functions by this method in a general case, but here is a solution without any additional imports.一般情况下不建议通过这种方法获取函数,但这里有一个没有任何额外导入的解决方案。 Python has a
getattr
builtin function which "[r]eturn the value of the named attribute of object." Python 有一个
getattr
内置函数,它“[r] 返回对象的命名属性的值”。 Its usage is getattr(object, name[, default])
.它的用法是
getattr(object, name[, default])
。 So you need to rewrite your function as the following.所以你需要重写你的函数如下。
def something(dataframe,col_name,func):
return getattr(dataframe[col_name], func)
something(df,'a',"sum")
If you want to get the result of the function call sum, replace the function definition with return getattr(dataframe.col_name, func)()
.如果要获取函数调用 sum 的结果,请将函数定义替换为
return getattr(dataframe.col_name, func)()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.