现在我和熊猫有点麻烦。。。在
我试图定义以下函数:
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'])
作为参考,以下是与OP在评论中讨论的答案:
问题是你用错了
pipe
。pipe
的第一个参数应该是一个函数。有关详细信息,请参见the official documentation:你必须找到另一种方法来创建你的数据帧。在
最简单的解决办法就是把它排除在外。在此上下文中不需要使用
pipe
。只需坚持您的示例并将其放入outlier
-函数中。然后返回结果数据帧。在相关问题 更多 >
编程相关推荐