我正在尝试比较两列 - 主列和辅助列。辅助列可能具有(。
I learned that to replace ("."), it has to be passed with (".")
如果辅助列具有特定的值,例如" Notapplicable here",那么我将结果视为true。
为此,我创建了一个称为 -
的变量异常=" Notapplicable Here"
,以下代码是我写的,
temp_result_df[res_col_name] = (temp_result_df[primaryreportreqcolname].eq(temp_result_df[RequiredSecondaryReport_Col_Name].str.replace
('.'|' (On Leave)', '', regex = True)) | (temp_result_df[RequiredSecondaryReport_Col_Name]== Exceptions))
且失败的错误说 - |'str'and str'。
PrimaryColumn SecondaryColumn ExpectedOutput
Mr Mr. True
Jr Jr True
Mrs Mrs True
Mr Mrs False
Mr Mr (On Leave) True
Mr NOTAPPLICABLEHERE True
请帮助我。
在我看来是有条件问题 - 需要逃脱()
,并在|
周围删除''
:
p = 'PrimaryColumn'
s = 'SecondaryColumn'
Exceptions = "NOTAPPLICABLEHERE"
df['new'] = df[p].eq(df[s].str.replace(r'.| (On Leave)', '')) | (df[s] == Exceptions)
或:
df['new'] = df[p].eq(df[s].replace(r'.| (On Leave)', '', regex = True)) |
(df[s ]== Exceptions)
print (df)
PrimaryColumn SecondaryColumn ExpectedOutput new
0 Mr Mr. True True
1 Jr Jr True True
2 Mrs Mrs True True
3 Mr Mrs False False
4 Mr Mr (On Leave) True True
5 Mr NOTAPPLICABLEHERE True True