lundi 8 mars 2021

Nested loop - alternative solution - Python pandas

I have started learning python, and I wanted to ask if there is an alternative faster solution to the below nested loop:

for y in range(total_rows2):
    for x in range(total_rows1):
        if df2.iloc[y,0]==df1.iloc[x,0]:
            df1.iloc[x,1]=df1.iloc[x,1]+df2.iloc[y,17]

Basically, I have found the number of rows (total_rows1 and total_rows2) of two dataframes (df1 and df2). The first column of both dataframes (index=0) correspond to some IDs.

If the IDs of the two dataframes match, then I want to add the value of column 18 of df2(index 17, column name='Profit') to the second column of df1 (index=1, column name='Profit')

df2:

ID Profit
0 0104 0
1 0106 0
2 0107 0
3 0108 0

df1:

ID Loss Profit
0 0104 100 230
1 0106 200 150
2 0108 150 120
3 0107 120 230
4 0109 100 400

Thanks!

Aucun commentaire:

Enregistrer un commentaire