Reputation: 380
I'm very new to Python. How can I match one text dataframe to another? (kindly please edit this question if I ask this wrongly)
For example given this input data:
df1 =
id Names
0 123 Simpson J.
1 456 Snoop Dogg
df2 =
Names
0 John Simpson
1 Snoop Dogg
2 S. Dogg
3 Mr Dogg
Is there a way I could find (maybe using findall
or match
, or any python packages) so that I could produce how many times the names with the id has appeared which almost like this result:
result =
id Names_appeared
0 123 1
1 456 3
Looking for a brief explanation and some URL to help me understand.
Upvotes: 0
Views: 315
Reputation: 3493
Here's an example using fuzzywuzzy as IanS suggested:
import pandas as pd
from fuzzywuzzy import fuzz
def fuzz_count(shortList, longList, minScore):
""" Count fuzz ratios greater than or equal to a minimum score. """
results = []
for s in shortList:
scores = [fuzz.ratio(s, l) for l in longList]
count = sum(x >= minScore for x in scores)
results.append(count)
return results
data1 = {'id': pd.Series([123, 456]),
'Names': pd.Series(['Simpson J.', 'Snoop Dogg'])}
data2 = {'Names': pd.Series(['John Simpson', 'Snoop Dogg', 'S. Dogg', 'Mr Dogg'])}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
result = pd.DataFrame()
result['id'] = df1['id']
counts = fuzz_count(df1['Names'], df2['Names'], minScore=60) # [1, 2]
result['Names_appeared'] = counts
print(result) # id Names_appeared
# 0 123 1
# 1 456 2
Upvotes: 1