问题描述
我有一个合并的数据框,其中我想对照“ 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)