简体   繁体   中英

pandas - If partial string match exists, put value in new column

I've got a tricky problem in pandas to solve. I was previously referred to this thread as a solution but it is not what I am looking for.

Take this example dataframe with two columns:

df = pd.DataFrame([['Mexico', 'Chile'], ['Nicaragua', 'Nica'], ['Colombia', 'Mex']], columns = ["col1", "col2"])

I first want to check each row in column 2 to see if that value exists in column 1. This is checking full and partial strings.

df['compare'] = df['col2'].apply(lambda x: 'Yes' if df['col1'].str.contains(x).any() else 'No')

I can check to see that I have a match of a partial or full string, which is good but not quite what I need. Here is what the dataframe looks like now:

在此处输入图像描述

What I really want is the value from column 1 which the value in column 2 matched with. I have not been able to figure out how to associate them

My desired result looks like this:

在此处输入图像描述

Here's a "pandas-less" way to do it. Probably not very efficient but it gets the job done:

def compare_cols(match_col, partial_col):
    series = []
    for partial_str in partial_col:
        for match_str in match_col:
            if partial_str in match_str:
                series.append(match_str)
                break  # matches to the first value found in match_col
        else:  # for loop did not break = no match found
                series.append(None)
    return series

df = pd.DataFrame([['Mexico', 'Chile'], ['Nicaragua', 'Nica'], ['Colombia', 'Mex']], columns = ["col1", "col2"])

df['compare'] = compare_cols(match_col=df.col1, partial_col=df.col2)

Note that if a string in col2 matches to more than one string in col1 , the first occurrence is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM