简体   繁体   English

Python/Pandas:在用户定义函数中使用内置函数作为参数

[英]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.sumpd.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.

相关问题 如何知道Python是内置的还是用户定义的异常 - How to know if exception is built-in or user-defined in Python 内置和用户定义类型之间的Python比较 - Python comparison between built-in and user-defined types 有没有一种方法可以使用户定义的Python函数像内置语句一样起作用? - Is there a way to make a user-defined Python function act like a built-in statement? Python:将带有参数的函数传递给内置函数? - Python: Passing functions with arguments to a built-in function? 如何选择将为内置min()和max()函数求值的用户定义类的属性? - How to choose the attribute of a user-defined class that will be evaluated for built-in min() and max() functions? 如何分辨用户定义的类和Python 3中的内置类之间的区别? - How to tell the difference between a user-defined class and a built-in in Python 3? 口译员:Python内置函数未定义吗? - Interpreter: Python built-in functions not defined? 内置函数比用户定义的函数需要更多的执行时间? (Python) - Built-in functions take more execution time than user defined functions? (Python) python中的用户自定义命令行arguments - User-defined command line arguments in python 使用用户定义的函数声明一个熊猫系列 - Declaring a pandas series with a user-defined function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM