How can I avoid overwriting in for-loops in Spyder (Python)?

Viewed 23

I have two Excel sheets. In each sheet I have a column with ID numbers and a column with results. If the ID numbers are identical from these two sheets, I want to add the results of those ID numbers to the first sheet into a new column called new_Resultat2. This is basically what my code is doing, but it always overwrites the value from the loop before. How can I avoid this?

df1 looks like this: enter image description here

and df2 like this: enter image description here

This is my current code:

for i in df2['ID2']:

    for j in df1['ID1']:
        if j == i:
            # print(df2.Resultat2[df2.ID2 == i]) 
            df1['new_Resultat2'] = df2.Resultat2[df2.ID2 == i]
            print(df1['new_Resultat2'])
0 Answers
Related