I'm trying to get the count of words (strings) in a pandas dataframe column that are found in another column in any order.
I've tried the following, which is close, but it doesn't count the occurrences (It only tells me if the words were found in any order).
words='|'.join(df['Cluster Name'].unique())
df['frequency']=df['Keyword'].str.contains(words).astype(int)
Minimum Reproducible Example:
data = {'Keyword' : ['Nike', 'Nike Socks', 'Nike Stripy Socks', 'Socks Nike', 'Adidas Socks'],
'Cluster' : ['Nike Socks', 'Nike Socks', 'Nike Socks', 'Nike Socks', 'Nike Socks']}
# Create DataFrame
df = pd.DataFrame(data)
expected output
Keyword Cluster Frequency
0 Nike Nike Socks 1
1 Nike Socks Nike Socks 2
2 Nike Stripy Socks Nike Socks 2
3 Socks Nike Nike Socks 2
4 Adidas Socks Nike Socks 1