【问题标题】:How do you check if data in one column is present in another column in Pandas?如何检查 Pandas 中的一列中的数据是否存在于另一列中?
【发布时间】:2020-10-01 19:14:10
【问题描述】:

我有一个包含“位置”和“职位”两列的数据框。我需要检查 Job Title 中的哪些行中包含 Locations 的名称。

        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

Location 中有 625 行,Job Location 中有接近一百万行。

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

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

【问题讨论】:

标签: python pandas numpy filtering


【解决方案1】:

这能回答你的问题吗?:

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

这是逐行子字符串检查(即,在同一行的职位名称中检查每一行的位置)。如果您想对照所有地点检查所有职位,请告诉我们,我很乐意进行相应的编辑。

【讨论】:

    【解决方案2】:

    您可以使用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']
    

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2022-12-12
      • 2019-02-22
      • 2022-01-18
      • 2020-06-21
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      相关资源
      最近更新 更多