0

I have two dataframes looking something like this

df1

Reference     New

1875          3756
1657          3998
3373          3112
3299          3090

df2

X           Replaced_X

1875        3756  
1657        3998  
1111        ...
1234          

I want to compare X from df2 with Reference from df1, and if these values are the same, I want to copy the corresponding New values to the **Replaced_X" column.

I am currently stuck with this code (the check for 'N' is because of NaT, also the comparing and copying should only happen if the values in X start with 1).

for index, row in df2.iterrows():
    first_dig_str = str(df2.X)
    if not 'N' in first_dig_str:
        first_dig = int(first_dig_str[0])
        if first_dig == 1:
            compare_row = df1.loc[df1['Reference'] == df2['X']]
            
            df2['Replaced_X'] = compare_row['New'] #This is where I struggle

What is the correct way to do this operation?

fendrbud
  • 79
  • 9

0 Answers0