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

2024-05-23 14:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

        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下没有值

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


Tags: andofdfnewtitlemanagerjoblocation
2条回答

这回答了你的问题吗

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']

相关问题 更多 >