2024-10-01 22:35:24 发布
网友
我需要向pd.rolling_apply的目标函数传递几个参数
我的目标函数是:
def complexFunction(i,j,k,l,verbosity=False): ... return 0.0
这些论点是位置性的吗?什么是规范的方法?在
根据您链接的documentation,您可以使用args关键字传递参数,第一个参数将由rolling_apply传入,您可以将其余参数定义为元组并将其传递到args关键字参数中。示例-
args
rolling_apply
pd.rolling_apply(df,<window>,complexFunction,args=(j,k,l))
示例/演示-
下面是从functools使用partial的方法。在
functools
partial
from functools import partial def complex_function(data, x, y): # some calculations return sum(data) * x * y my_partial_func = partial(complex_function, x=2, y=5) ser = pd.Series(np.arange(10)) pd.rolling_apply(ser, window=5, func=my_partial_func) 0 NaN 1 NaN 2 NaN 3 NaN 4 100 5 150 6 200 7 250 8 300 9 350 dtype: float64
我通常让我的函数以一行作为它的单一输入
def complexFunction(row,verbosity=False): i = row.i j = row.j k = row.k return 0.0 pd.rolling_apply(df, complexFunction)
根据您链接的documentation,您可以使用
args
关键字传递参数,第一个参数将由rolling_apply
传入,您可以将其余参数定义为元组并将其传递到args
关键字参数中。示例-示例/演示-
^{pr2}$下面是从
functools
使用partial
的方法。在我通常让我的函数以一行作为它的单一输入
相关问题 更多 >
编程相关推荐