我已经看过以下答案:How can I selectively escape percent (%) in Python strings?而且我相信我的问题有所不同。

我正在尝试将百分比转换为十进制,但首先我需要测试百分比符号是否存在。我的数据在dataframe中。我的代码是:

df[i]=df[i].map(lambda x: float(x.strip('%'))/100 if '%' in x)

这给了我SyntaxError: invalid syntax

最佳答案

看来您需要像这样的else中的lambda

lambda x: float(x.strip('%'))/100 if '%' in x else x

关于python - Python测试字符串是否具有“%”且没有中断,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24472905/

10-14 18:31
查看更多