Skip to content Skip to sidebar Skip to footer

Pandas Series Case-insensitive Matching And Partial Matching Between Values

I have the following operation to add a status showing where any string in a column of one dataframe column is present in a specified column of another dataframe. It looks like thi

Solution 1:

You can do the first test by converting both strings to lowercase or uppercase (either works) inside the expression (as you aren't reassigning either column back to your DataFrames, the case conversion is only temporary):

df_one['Status'] = np.where(df_one.A.str.lower().isin(df_two.A.str.lower()), \ 
                            'Matched', 'Unmatched')

You can perform your second test by checking whether each string in df_one.A ends with any of the strings in df_two.A, like so (assuming you still want a case-insensitive match):

df_one['Endswith_Status'] = np.where(df_one.A.str.lower().apply( \
                                      lambda x: any(x.endswith(i) for i in df_two.A.str.lower())), \ 
                                      'Matched', 'Unmatched')

Post a Comment for "Pandas Series Case-insensitive Matching And Partial Matching Between Values"