I want to match two string columns and count number of (exact) matches. For example, there are two columns like
index col1 col2
0 aa ji
1 bs aa
2 qe bs
3 gd aa
col1 consists of unique ids. I want to count how many times each element of col1 occurs in col2. In other words, I would like to get an output like:
col3
2
1
0
0
in above example.
I have tried above work using pandas str.contains() and for loop, but given a large number of observations, it seems too slow and inefficient. My code looks like below.
num = []
for i in range(len(col1)):
count = col2.str.contains(col1[i]).sum()
num_replies.append(count)
Is there a time-efficient way to do this work?