'Append columns to DataFrame form another DataFrame
everyone! Can you pleas help me with the bellow! I have the first df_1:
| key | end |
|---|---|
| 1 | 10 |
| 1 | 20 |
| 2 | 30 |
| 2 | 40 |
And the second df_2:
| key | time |
|---|---|
| 1 | 13 |
| 1 | 25 |
| 2 | 35 |
| 2 | 45 |
I need add columns from df_1 to df_2 with the condition: df_1['key'] == df_2['key'] and df_2['time'] > df_1['end']
The final solution should look like:
| key | time | end_1 | end_2 |
|---|---|---|---|
| 1 | 13 | 10 | |
| 1 | 25 | 10 | 20 |
| 2 | 35 | 30 | |
| 2 | 45 | 30 | 40 |
I was thinking to solve it like on the example bellow:
for index_1, row_1 in df_2.iterrows():
for index_2, row_2 in df_1.iterrows():
if row_1[0] == row_2[0] and row_1[1] > row_2[2]:
row_1.append(row_2)
But it doesn't work
I would appreciate if someone could help me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
