Python 如何检查Pandas中一列中的数据是否存在于另一列中?

Python 如何检查Pandas中一列中的数据是否存在于另一列中?,python,pandas,numpy,filtering,Python,Pandas,Numpy,Filtering,我有一个数据框,有两列“位置”和“职务”。我需要检查职务标题中的哪些行中有位置的名称 Location Job Title 0 New York New York Regional Manager Las Vegas and San Diego 1 New York City Full Stack Engineer 2 San Francisco Bay Area Director of Guitar Studies 3 Greater Los A

我有一个数据框,有两列“位置”和“职务”。我需要检查职务标题中的哪些行中有位置的名称

        Location    Job Title
0   New York New York   Regional Manager Las Vegas and San Diego
1   New York City   Full Stack Engineer
2   San Francisco Bay Area  Director of Guitar Studies
3   Greater Los Angeles New England Institute of Technology
4   Greater Chicago New England Institute of Technology
... ... ...
984710  NaN Catering Sales Manager
984711  NaN Director, Research & Development and
984712  NaN HR Manager
984713  NaN Director of Development
984714  NaN Development Officer
位置中有625行,工作位置中有近一百万行

我尝试了
df['exist1']=df['Location'].isin(df['Job Title'])
之后,我尝试根据真值对其进行过滤,但它将625以下的所有值都显示为真。位置列中625下没有值


我哪里做错了?任何帮助都将不胜感激

这是否回答了您的问题

df['exist1'] = df.apply(lambda x: x['Location'] in x['Job Title'], axis=1)

这是逐行子字符串检查(即在同一行的职务中检查每一行的位置)。如果您想根据所有位置检查所有职务,请告知我们,我很乐意对其进行相应编辑。

这是否回答了您的问题

df['exist1'] = df.apply(lambda x: x['Location'] in x['Job Title'], axis=1)

这是逐行子字符串检查(即在同一行的职务中检查每一行的位置)。如果您想根据所有位置检查所有职务,请告知我们,我很乐意对其进行相应编辑。

您可以使用
str.contains

df['exist1'] = df['Location'].str.contains('|'.join(df['Job Title'].dropna().tolist()))
如果您想为每一行匹配

df1=df.dropna()
df1['exist1'] = [ x in y for x, y  in zip(df1['Location'], df1['Job Title'])]
df['exist1']=df1['exist1']

您可以使用
str.contains

df['exist1'] = df['Location'].str.contains('|'.join(df['Job Title'].dropna().tolist()))
如果您想为每一行匹配

df1=df.dropna()
df1['exist1'] = [ x in y for x, y  in zip(df1['Location'], df1['Job Title'])]
df['exist1']=df1['exist1']

这需要用for loop或numpy cherry charIf来完成。你能告诉我这能回答你的问题吗?这需要用for loop或numpy cherry charIf来完成。你能告诉我这能回答你的问题吗?