如果 str1 in row of col==str in row of another col, set str1=str2 of third col

If str1 in row of col==str in row of another col, set str1=str2 of third col

我有一个合并的数据框,我想在其中检查 'profile' 列的行中包含的每个字符串与 'right_side' 列的行中的每个字符串。如果这两个字符串相等,我想用 'left_side' 列中的 str 替换 'profile' 列中的 str 。由于某种原因,我在这方面遇到了很多困难,并尝试了 .loc、dict、map 和 .at 方法,但必须缺少一个组件,每个组件都无法正常工作。这是数据框:

profile             left_side                       right_side
DevOps Developer    NaN                             NaN     
Software Developer  Software Development Developer  Software Developer
Software Developer  Software Development Developer  Software Developer
Quality Assurance   Quality Assurance Engineer      Quality Assurance

非常感谢您的帮助!

df.loc[(df["Profile"]==df["right_side"]),"Profile"]=df["left_side"]
print(df)

                          Profile  ...          right_side
0                DevOps Developer  ...                 NaN
1  Software Development Developer  ...  Software Developer
2  Software Development Developer  ...  Software Developer
3      Quality Assurance Engineer  ...   Quality Assurance

[4 rows x 3 columns]

这是你想要的吗?

这是我使用 lambda 的尝试:

data["profile"] = data.apply(lambda x: x[1] if x[0] == x[2] else x[0], axis=1)