【发布时间】:2020-12-24 15:09:27
【问题描述】:
我正在尝试创建一个接受多个参数的函数,但它似乎不起作用。当前代码是:
def atomic(x):
if np.in1d(x['concept:name'], 'W_Completeren aanvraag').sum() > 3:
x['result'] = True
else:
x['result'] = False
return x
df.groupby(['case:concept:name']).apply(atomic)
此代码将 'result' 列添加到 df,其值为 True 或 False,具体取决于 if 语句的结果。
我想在 if 语句中看到变量,即 'x['concept:name']' 和 'W_Completeren aanvraag' 参数。所以解决方案有点像这样:
def atomic(x, key, check):
if np.in1d(x[key], check).sum() > 3:
x['result'] = True
else:
x['result'] = False
return x
df.groupby(['case:concept:name']).apply(atomic('concept:name', 'W_Completeren aanvraag'))
但是,当我像这样编写脚本时,x 变量是不可调用的,但如果不首先为参数“x”分配一个值,我似乎无法弄清楚它为什么会起作用。它给出了错误TypeError: atomic() missing 1 required positional argument: 'x'
有人有想法吗?
【问题讨论】:
标签: python pandas group-by parameter-passing