Search code examples
pythonpandasreplacepandas-loc

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


I have a merged dataframe in which I would like to check each string contained in the row of the 'profile' column against each string in the row of the 'right_side' col. If these two strings are equal, I would like to replace the str in the 'profile' column with the str in the 'left_side' column. Am having a really hard time with this fro some reason and have tried, .loc, dict, map and .at methods but must be missing one component with each as nothing as worked. Here is the dataframe:

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

Thank you so much for the help!


Solution

  • 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]
    

    Is this what you were after?