Pandas 如果列的行中的str1==另一列的行中的str,则设置第三列的str1=str2
我有一个合并的数据框,在该数据框中,我想对照“right_side”列中的每个字符串检查“profile”列行中包含的每个字符串。如果这两个字符串相等,我想用“left_side”列中的str替换“profile”列中的str。出于某种原因,我对此感到非常困难,并尝试了.loc、dict、map和.at方法,但每个方法都必须缺少一个组件,因为它们都没有工作。以下是数据帧:Pandas 如果列的行中的str1==另一列的行中的str,则设置第三列的str1=str2,pandas,replace,loc,Pandas,Replace,Loc,我有一个合并的数据框,在该数据框中,我想对照“right_side”列中的每个字符串检查“profile”列行中包含的每个字符串。如果这两个字符串相等,我想用“left_side”列中的str替换“profile”列中的str。出于某种原因,我对此感到非常困难,并尝试了.loc、dict、map和.at方法,但每个方法都必须缺少一个组件,因为它们都没有工作。以下是数据帧: profile left_side right_side
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)
以下是我使用lambda的尝试:
data["profile"] = data.apply(lambda x: x[1] if x[0] == x[2] else x[0], axis=1)
嗨,你能提供你预期结果的样本表吗?嗨,你能提供你预期结果的样本表吗?哦,天哪,是的。我尝试了:df.loc[df.profile.isin(df.right_side),[“left_side”]=df[“left_side”],结果很接近,但不太接近。非常感谢。哦,天哪,是的。我尝试了:df.loc[df.profile.isin(df.right_side),[“left_side”]=df[“left_side”],结果很接近,但不太接近。非常感谢。