gpt4 book ai didi

python - 识别 pandas 列中的某些单词

转载 作者:行者123 更新时间:2023-11-28 17:10:56 25 4
gpt4 key购买 nike

我有一个 tsv 文件如下。

id    ingredients    recipe
code1 egg, butter beat eggs. add unsalted butter
code2 tim tam, butter beat tim tam. add butter
code3 coffee, sugar add coffee and sugar and mix
code4 sugar, fresh goat milk beat sugar and milk together

如果条目在 ingredientsrecipe 列中包含以下提到的词,我想删除这些条目。

mylist = ['tim tam', 'unsalted butter', 'fresh goat milk']

我的输出应该如下所示。

id    ingredients    recipe
code3 coffee, sugar add coffee and sugar and mix

有没有办法使用 Pandas 来做到这一点?请帮助我!

最佳答案

使用 containsjoin 查看字符串是否包含“子”字符串,并使用 '|' 连接基数制作正则表达式:

mylist = ['tim tam','unsalted butter','fresh goat milk']
df[~(df.ingredients.str.contains('|'.join(mylist)) |
df.recipe.str.contains('|'.join(mylist)))]

输出:

     id    ingredients                        recipe
2 code3 coffee, sugar add coffee and sugar and mix

关于python - 识别 pandas 列中的某些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47514330/

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