how to compare two string variables in pandas?
I think you can use str.lower
and str.replace
with arbitrary whitespace s/+
:
test = pd.DataFrame({'A': ["john doe", " john doe", 'John'],
'B': [' john doe', 'eddie murphy', 'batman']})
print test['A'].str.lower().str.replace('s/+',"") ==
test['B'].str.strip().str.replace('s/+',"")
0 True
1 False
2 False
dtype: bool
strip
the spaces and lower
the case:
In [414]:
test['A'].str.strip().str.lower() == test['B'].str.strip().str.lower()
Out[414]:
0 True
1 False
2 False
dtype: bool
You can use difflib to compute distance
import difflib as dfl
dfl.SequenceMatcher(None,'John Doe', 'John doe').ratio()
edit : integration with Pandas :
import pandas as pd
import difflib as dfl
df = pd.DataFrame({'A': ["john doe", " john doe", 'John'], 'B': [' john doe', 'eddie murphy', 'batman']})
df['VAR1'] = df.apply(lambda x : dfl.SequenceMatcher(None, x['A'], x['B']).ratio(),axis=1)