繁体   English   中英

Pandas lambda function 语法错误(带字典)

[英]Pandas lambda function syntax error (with dictionary)

我在 Python 中经常使用 lambda 函数。 突然之间,我不明白为什么会出现语法错误消息:

table['sp1 name'] = table['sp1'].apply(lambda x: sp1_new_dict[x] if x in sp1_new_dict.keys())

有任何想法吗? 谢谢!

你需要一个else 归结你的错误:

x = 1 if True

  File "<stdin>", line 1
    x = 1 if True
                 ^
SyntaxError: invalid syntax


# No error here
x = 1 if True else 2

由于您使用的是字典,因此可以使用dict.get

table['sp1 name'] = table['sp1'].apply(lambda x: sp1_new_dict.get(x))

如果密钥不存在,则返回None

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM