gpt4 book ai didi

python - 如何检查字符串中是否包含某个单词?

转载 作者:行者123 更新时间:2023-12-02 05:47:57 29 4
gpt4 key购买 nike

我有一个类似这样的系列:

0          1.5 
1 39 mins
2 2.5
3 3

我想将 39 分钟转换为 0.39。以下是我的代码:

df['content_duration'].apply(lambda x: x.str.replace('mins', '').astype(int) * 1/100 if x in 'mins' else x)

我没有收到错误,但它仍然不会转换它。我怎样才能使这成为可能?我的预期输出是:

0          1.5 
1 0.39
2 2.5
3 3

最佳答案

这对我有用:

# dataset for read_clipboard()
'''
content_duration
1.5
39 mins
2.5
3
'''

df = pd.read_clipboard('\t')

df['content_duration'] = df['content_duration'].apply(lambda x: int(x.replace('mins', '').strip()) * 1/100 if 'mins' in x else x)


print(df)

输出:

  content_duration
0 1.5
1 0.39
2 2.5
3 3

关于python - 如何检查字符串中是否包含某个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61844248/

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