Pandas测向管()关键字不能是表达式

2024-05-29 04:08:34 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我和熊猫有点麻烦。。。在

我试图定义以下函数:

def outlier(frame, col1, col2):
    (frame.pipe(frame.loc[:,"lower"] = frame.groupby(by='col1')['col2'].transform(lower))
     frame.pipe(frame.loc[:,"upper"] = frame.groupby(by='col1')['col2'].transform(upper))
     frame.pipe(frame.loc[:,"outlier"] = (frame['col2'] < frame['lower'])|(frame['col2'] > frame['upper']))
    )

不幸的是我得到了: SyntaxError:关键字不能是表达式

有些东西使我不能以牙还牙,也不能发现我明显愚蠢的错误。。。在

在以下代码中它起作用:

这是“上”和“下”的功能:

^{pr2}$

这是我的“意大利面代码”,用于将新列添加到数据帧:

acid81.loc[:,"lower"] = acid81.groupby(by='Ursprung')['Analysenwert'].transform(lower)
acid81.loc[:,"upper"] = acid81.groupby(by='Ursprung')['Analysenwert'].transform(upper)
acid81.loc[:,"outlier"] = (acid81['Analysenwert'] < acid81['lower']) | (acid81['Analysenwert'] > acid81['upper'])

Tags: 代码bytransformupperlowerframeloccol2
1条回答
网友
1楼 · 发布于 2024-05-29 04:08:34

作为参考,以下是与OP在评论中讨论的答案:

问题是你用错了pipepipe的第一个参数应该是一个函数。有关详细信息,请参见the official documentation

Parameters:

func : function

function to apply to the NDFrame. args, and kwargs are passed into func. Alternatively a (callable, data_keyword) tuple where data_keyword is a string indicating the keyword of callable that expects the NDFrame.

args : positional arguments passed into func.

kwargs : a dictionary of keyword arguments passed into func.

你必须找到另一种方法来创建你的数据帧。在

最简单的解决办法就是把它排除在外。在此上下文中不需要使用pipe。只需坚持您的示例并将其放入outlier-函数中。然后返回结果数据帧。在

相关问题 更多 >

    热门问题