我有一个dataframe,其中我试图匹配两个列的列字符串值,以创建一个新列,如果两个列值匹配,则返回true;如果不匹配,则返回false。
pattern = re.compile('^a-zA-Z')
Name A Name B
0 yGZ,) ygz.
1 (CGI) C.G.I
2 Exto exto.
3 Golden UTF我想试试这样的东西:
dataframe['Name A', 'Name B'].str.match(pattern, flags= re.IGNORECASE)
Name A Name B Result
0 yGZ,) ygz. True
1 (CGI) C.G.I True
2 Exto exto. True
3 Golden UTF False发布于 2019-04-09 12:39:01
您可以使用str.replace删除标点符号(也可以参见我的另一篇文章Fast punctuation removal with pandas),然后
u = df.apply(lambda x: x.str.replace(r'[^\w]', '').str.lower())
df['Result'] = u['Name A'] == u['Name B']
df
Name A Name B Result
0 yGZ,) ygz. True
1 (CGI) C.G.I True
2 Exto exto. True
3 Golden UTF False发布于 2019-04-09 12:43:20
可以使用pd.DataFrame.replace清理字符串,然后使用eq进行比较。当然,如果您希望维护原始df的副本,只需将返回的数据框赋给一个新变量;}
df = df.replace("[^a-zA-Z0-9]", '', regex=True)然后
df['Result'] = df['Name A'].str.lower().eq(df['Name B'].str.lower())输出
Name A Name B Result
0 yGZ ygz True
1 CGI CGI True
2 Exto exto True
3 Golden UTF Falsehttps://stackoverflow.com/questions/55585189
复制相似问题