检查一列是否包含 python pandas 中另一列的数据

Check if a column contains data from another column in python pandas

我在 pandas 中有一个像这样的数据框

name     url
pau lola www.paulola.com
pou gine www.cheeseham.com
pete raj www.pataraj.com

我想检查列名中的任何字符串是否在列 url 中(因此忽略空格)。所以像这样

name     url                result
pau lola www.paulola.com    True
pou gine www.cheeseham.com  False
pete raj www.pataraj.com    True

有什么办法吗?我尝试使用此 lambda 函数,但仅当同时包含两者时才有效

name     url               namewospaces
pau lola www.paulola.com   paulola
pou gine www.cheeseham.com pougine
pete raj www.pataraj.com   peteraj

df['result'] = df.apply(lambda x: str(x.namewospaces) in str(x.url), axis=1)

name     url               namewospaces  result
pau lola www.paulola.com   paulola       True
pou gine www.cheeseham.com pougine       False
pete raj www.pataraj.com   peteraj       False

谢谢大家:)

split 将名称转换为子字符串,并使用带有 any 的列表理解来获得 True is any string matches:

df['result'] = [any(s in url for s in lst)
                for lst, url in zip(df['name'].str.split(), df['url'])]

apply 等价的(较慢的)将是:

df['result'] = df.apply(lambda x: any(s in x['url']
                        for s in x['name'].split()), axis=1)

输出:

       name                url  result
0  pau lola    www.paulola.com    True
1  pou gine  www.cheeseham.com   False
2  pete raj    www.pataraj.com    True