Take a sample dataset:
df = pd.DataFrame([['Mexico', 'Chile'], ['Nicaragua', 'Nica'], ['Colombia', 'Mex']], columns = ["col1", "col2"])
The dataframe looks like this:
I have two columns. I want to check to see if the values in column two exist in column one. This includes checking for partial strings.
The desired output is:
I am able to compare the whole value of each row in column two, but this does not account for partial strings:
df['compare'] = np.where(df['col2'].isin(df['col1']), 'yes', 'no')
I am also able to check if a single value exists within a column, which checks for partial strings but does not include every row in the 'col2' column.
df['compare'] = df['col1'].str.contains('Mex')
How can I do both at the same time?
