Python Pandas Lambda:在DataFrame中使用多个变量Lambda

5

我有一个如下所示的系列:

example = pd.Series([[1.0, 1209.75, 1207.25],
 [1.0, 1211.0, 1207.5],
 [-1.0, 1211.25, 1205.75],
 [0, 1207.25, 1206.0],
 [1.0, 1206.25, 1201.0],
 [-1.0, 1205.75, 1202.75],
 [0, 1205.5, 1203.75]])

这个系列基本上是每个单元格中有3个数字的列表。 我将其转换为DataFrame并添加了一个新列:

example = example.to_frame(name="input")
example["result"]=np.NaN

现在我想对它执行以下操作:
example["result"] = example["input"].apply(lambda x,y,z: y if x==1 else z if x==-1 else NaN)

尝试执行时,我收到以下错误消息: 缺少 2 个必需的位置参数:'y' 和 'z'
2个回答

6
Lambda只接受一个参数,这里的参数是一个列表。只需对列表进行索引即可:
>>> example["result"] = example["input"].apply(lambda lst: lst[1] if lst[0]==1 else lst[2] if lst[0]==-1 else np.NaN)
>>> example
                      input   result
0   [1.0, 1209.75, 1207.25]  1209.75
1     [1.0, 1211.0, 1207.5]  1211.00
2  [-1.0, 1211.25, 1205.75]  1205.75
3      [0, 1207.25, 1206.0]      NaN
4    [1.0, 1206.25, 1201.0]  1206.25
5  [-1.0, 1205.75, 1202.75]  1202.75
6      [0, 1205.5, 1203.75]      NaN

轻松一点,您可以将嵌套的三元运算符重构为带有嵌套if语句的函数,以使您的代码更易读:

def func(lst):
    x, y, z = lst
    if x == 1:
        return y
    elif x == -1:
        return z
    else:
        return np.NaN


example["result"] = example["input"].apply(func)

是的,我刚刚也发现了...抱歉各位。但有趣的是,很多时候只要用不同的措辞提问,我就能找到答案...无论如何,谢谢!你的评论是什么意思?你有什么建议呢? - jim jarnac
非常感谢。在函数的情况下,为什么我们使用x、y、z而不是像lambda中那样使用x[0]、x[1]、x[2]?函数和lambda不应该是等价的吗? - jim jarnac
我将lst作为参数传递,而不是x。这只是一个名称的更改。 - Moses Koledoye
好的,是的,我漏掉了那个。谢谢。 - jim jarnac

0
这是一个矢量化的解决方案:
In [30]: example
Out[30]:
                      input
0   [1.0, 1209.75, 1207.25]
1     [1.0, 1211.0, 1207.5]
2  [-1.0, 1211.25, 1205.75]
3      [0, 1207.25, 1206.0]
4    [1.0, 1206.25, 1201.0]
5  [-1.0, 1205.75, 1202.75]
6      [0, 1205.5, 1203.75]

In [31]: example['result'] = np.where(np.isclose(example.input.str[0], 1),
    ...:                              example.input.str[1],
    ...:                              np.where(np.isclose(example.input.str[0], -1),
    ...:                                       example.input.str[2],
    ...:                                       np.nan))
    ...:

In [32]: example
Out[32]:
                      input   result
0   [1.0, 1209.75, 1207.25]  1209.75
1     [1.0, 1211.0, 1207.5]  1211.00
2  [-1.0, 1211.25, 1205.75]  1205.75
3      [0, 1207.25, 1206.0]      NaN
4    [1.0, 1206.25, 1201.0]  1206.25
5  [-1.0, 1205.75, 1202.75]  1202.75
6      [0, 1205.5, 1203.75]      NaN

这并未处理 example.str[0] 为 -1 的情况。 - Moses Koledoye
@MaxU 这很有趣,不过 .isclose 有点不幸。 - jim jarnac
@MosesKoledoye,感谢您指出这个问题!我已经更正了我的答案。 - MaxU - stand with Ukraine
@jimbasquiat,当使用float数据类型时,最好使用iscloseallclose来比较值。 - MaxU - stand with Ukraine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接