Python/Pandas: How to Match List of Strings with a DataFrame column

后端 未结 4 1828
离开以前
离开以前 2021-01-04 21:32

I want to compare two columnn -- Description and Employer. I want to see if any keywords in Employer are found in the Descriptio

4条回答
  •  粉色の甜心
    2021-01-04 22:02

    Here is a readable solution using an individual search_func:

    def search_func(row):
        matches = [test_value in row["Description"].lower() 
                   for test_value in row["Text_Search"]]
    
        if any(matches):
            return "Yes"
        else:
            return "No"
    

    This function is then applied row-wise:

    # create example data
    df = pd.DataFrame({"Description": ["CANSEL SURVEY E PAY", "JX154 TFR?FR xxx8690"],
                       "Employer": ["Cansel Survey Equipment", "Cansel Survey Equipment"]})
    
    print(df)
        Description             Employer
    0   CANSEL SURVEY E PAY     Cansel Survey Equipment
    1   JX154 TFR?FR xxx8690    Cansel Survey Equipment
    
    # create text searches and match column
    df["Text_Search"] = df["Employer"].str.lower().str.split()
    df["Match"] = df.apply(search_func, axis=1)
    
    # show result
    print(df)
        Description             Employer                    Text_Search                     Match
    0   CANSEL SURVEY E PAY     Cansel Survey Equipment     [cansel, survey, equipment]     Yes
    1   JX154 TFR?FR xxx8690    Cansel Survey Equipment     [cansel, survey, equipment]     No
    

提交回复
热议问题