gpt4 book ai didi

python - Pandas 过滤 : Returning True/False vs the Actual Value

转载 作者:行者123 更新时间:2023-11-28 20:13:57 25 4
gpt4 key购买 nike

我的数据框:

df_all_xml_mfiles_tgther

file_names searching_for everything
0 a.txt where Dave Ran Away. Where is Dave?
1 a.txt candy mmmm, candy
2 b.txt time We are looking for the book.
3 b.txt where where the red fern grows

我的问题:

我正在尝试筛选包含在我的搜索条件中找到的词的记录。我需要一次检查 1 条记录并返回实际记录,而不仅仅是单词 true。

我尝试过的:

search_content_array = ['where', 'candy', 'time']
file_names_only = ['a.txt', 'b.txt']


for cc in range(0, len(file_names_only), 1):
for bb in range(0, len(search_content_array), 1):

stuff = `df_all_xml_mfiles_tgther[cc:cc+1].everything.str.contains(search_content_array[bb], flags=re.IGNORECASE, na=False, regex=True)`

if not regex_stuff.empty:
regex_stuff_new = pd.DataFrame([regex_stuff.rename(None)])
regex_stuff_new.columns = ['everything']
regex_stuff_new['searched_for_found'] = search_content_array[bb]
regex_stuff_new['file_names'] = file_names_only[cc]

regex_stuff_new = regex_stuff_new[['file_names', 'searched_for_found', 'everything']] ##This rearranges the columns

df_regex_test = df_regex_test.append(regex_stuff_new, ignore_index=True, sort=False)

我得到的结果是这样的:

    file_names  searched_for_found  everything
0 a.txt where True
1 a.txt candy True
2 b.txt where True

我想要的结果是这样的:

    file_names  searched_for_found                           everything
0 a.txt where Dave Ran Away. Where is Dave?
1 a.txt candy mmmm, candy
3 b.txt where where the red fern grows

如何获取返回结果的实际值,而不仅仅是 true/false?

最佳答案

使用列表推导式按元素执行此操作。

df[[y.lower() in x.lower() for x, y in zip(df['everything'], df['searching_for'])]]

或者,

df[[y.lower() in x.lower() 
for x, y in df[['everything', 'searching_for']].values.tolist()]]


file_names searching_for everything
0 a.txt where Dave Ran Away. Where is Dave?
1 a.txt candy mmmm, candy
3 b.txt where where the red fern grows

关于python - Pandas 过滤 : Returning True/False vs the Actual Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51991966/

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