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'). An id may appear twice in df2 but I will appear the sum in df1 (please see below for id 0108). So the 'Profit' column of df1 will be the sum of Profit per ID.
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 |
| 5 | 0108 | 150 | 400 |
So I want df2 to look as followed:
| --- | ID | Profit |
|---|---|---|
| 0 | 0104 | 230 |
| 1 | 0106 | 150 |
| 2 | 0107 | 230 |
| 3 | 0108 | 520 |
Thanks!
df1.assign(col2=df2['col17'])