要让一点点走得更远,请执行以下操作:
- 为每一列创建一个新系列并将正则表达式模式
\W+ 传递给str.replace()
- 使用
str.lower()
- 创建替换列表以将
drive 标准化为dr、avenue 至ave 等。
s1 = df['A'].str.replace('\W+', '').str.lower()
s2 = df['B'].str.replace('\W+', '').str.lower()
lst = [*df[s1==s2]['A']]
lst
Out[1]: ['- 5923FoxRd', 'Saratoga Street, Suite 200']
这是 s1 和 s2 的样子:
print(s1,s2)
0 5923foxrd
1 631newhavenave
2 saratogastreetsuite200
Name: A, dtype: object
0 5923foxrd
1 modesto
2 saratogastreetsuite200
Name: B, dtype: object
从那里,您可能想要创建一些替换值,以便进一步规范化您的数据,例如:
to_replace = ['drive', 'avenue', 'street']
replaced = ['dr', 'ave', 'str']
to_replace = ['drive', 'avenue', 'street']
replaced = ['dr', 'ave', 'str']
s1 = df['A'].str.replace('\W+', '').str.lower().replace(to_replace, replaced, regex=True)
s2 = df['B'].str.replace('\W+', '').str.lower().replace(to_replace, replaced, regex=True)
lst = [*df[s1==s2]['A']]
lst
print(s1,s2)
0 5923foxrd
1 631newhavenave
2 saratogastrsuite200
Name: A, dtype: object
0 5923foxrd
1 modesto
2 saratogastrsuite200
Name: B, dtype: object