gpt4 book ai didi

python - Pandas - 搜索术语(无论搜索术语的大小写)

转载 作者:行者123 更新时间:2023-12-01 01:26:27 25 4
gpt4 key购买 nike

我有下面的代码,用于在整个数据帧中搜索字符串。

df[df.apply(lambda x: x.astype(str).str.contains(search)).any(axis=1)]

但是,我有一个问题,如果搜索团队使用大写字母,则会失败。有什么方法可以搜索整个 Dataframe,无论 Dataframe 中的搜索词是大写还是小写。

最佳答案

我相信您需要参数 flagsre.I 来忽略情况:

import re

df[df.apply(lambda x: x.astype(str).str.contains(search, flags=re.I)).any(axis=1)]

另一个解决方案是转换每一列以及搜索字符串:

df[df.apply(lambda x: x.astype(str).str.lower().str.contains(search.lower())).any(axis=1)]

关于python - Pandas - 搜索术语(无论搜索术语的大小写),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53300901/

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