gpt4 book ai didi

Python测试字符串是否有 "%"并且不让它中断

转载 作者:行者123 更新时间:2023-12-01 05:09:31 24 4
gpt4 key购买 nike

我看过这个答案:How can I selectively escape percent (%) in Python strings?我相信我的问题是不同的。

我正在尝试将百分比转换为小数,但首先我需要测试百分号是否存在。我的数据位于数据帧中。我的代码是:

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

这给了我SyntaxError:无效语法

最佳答案

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

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

关于Python测试字符串是否有 "%"并且不让它中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24472905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com