gpt4 book ai didi

python - 连续出现 pandas : if,,一列中的单词不会出现在其他列的字符串中,删除行

转载 作者:行者123 更新时间:2023-11-28 22:42:01 24 4
gpt4 key购买 nike

假设我们有这个数据框:

from pandas import *

d = {'one' : Series(["word", "other-word", "banana", "hello"]),
'two' : Series(["I like that word", "Have you seen other-word", "do you like bananas", "hello-kitty doll"])}

df = DataFrame(d)

如何删除 one 没有出现在 two 中的行?例如,在第三行 bananabananas 不匹配:删除行。第四个:hello 不匹配hello-kitty:掉。最后一个很重要:用连字符 - 构建的化合物是障碍。

预期输出:

          one                       two
0 word I like that word
1 other-word Have you seen other-word

最佳答案

你可以这样做:

result = []
for x, y in zip(df.one, df.two):
if x in y.split():
result.append(True)
continue
result.append(False)

print df[result]

更好的方法:

df[[ x in y.split() for x, y in zip(df.one, df.two) ]]

关于python - 连续出现 pandas : if,,一列中的单词不会出现在其他列的字符串中,删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32115777/

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