Python在使用功能替换文本的同时比较了Pandas DF中的两列



我有这样的df,

Cola    Colb
Mr      Mr..!
Mrs     Mrs.!.
Mr      Tests

我想比较这两列忽略(和!存在) - 我可以在更换不需要的字符的同时生成一个新列。但是,是否有更好的方法可以使用PANDAS功能?

所有3行的预期结果都是正确的。

这是我直接比较的一行代码

temp_result_df[res_col_name] = 
((temp_result_df[primaryreportreqcolname] == temp_result_df[RequiredSecondaryReport_Col_Name])
& (temp_result_df[RequiredSecondaryReport_Col_Name]!= 'Tests'))

Python的新手。因此,我正在探索与数据中一些噪声进行比较的不同功能和方法。

iiuc,

df['res_col_name'] = df['Cola'].eq(df['Colb'].replace('W+', '', regex = True))  | df['Colb'].eq('Tests')

    Cola    Colb    res_col_name
0   Mr      Mr..!   True
1   Mrs     Mrs.!.  True
2   Mr      Tests   True

最新更新