代码之家  ›  专栏  ›  技术社区  ›  KawaiKx

如何在另一个数据帧中搜索数据帧值

  •  -1
  • KawaiKx  · 技术社区  · 5 年前

    enter image description here

    我想从df2中删除不包含df1公司名称(子字符串)的行。 我该怎么做?

    3 回复  |  直到 5 年前
        1
  •  1
  •   tdy TheChimp    5 年前

    你可以查一下 .str.count() 属于 df1.COMPANY 中的子字符串 df2.COMPANY 要创建布尔掩码,请执行以下操作:

    mask = df2.COMPANY.str.count('|'.join(df1.COMPANY.values)).astype(bool)
    df2 = df2[mask]
    
        2
  •  0
  •   SequenceToSequence    5 年前

    看起来您想内部联接数据帧:

    pd.merge(df1, df2, on='COMPANY', how='inner')
    

    请检查此处的文档以进行合并: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

        3
  •  0
  •   Nk03    5 年前
    #extarct unique company list from 1st dataframe
    
    company_list = set(df1['company'].to_list())
    
    #fucntion to check if any unique string is present or not 
    def test_presnce(x,company_list):
        return(any(value for value in company_list if value in x))
    
    #company_present column can now be used to filter the required data
    df2['company_present'] = df2['company'].apply(test_presnce, args=(company_list,))