问题
I have two different dataframes: A, B. The column Event has similar data that I'm using to compare the two dataframes. I want to give Dataframe A a new column, dfA.newContext#.
In order to do this, I'll need to use the Event column. I want to iterate through Dataframe A to find a match for Event and assign the dfB.context# to dfA.newContext#
I think a loop would be the best way since I have a few conditions that I need to check.
This might be asking a bit much but I'm really stuck.. I want to do something like this, because I'll need to take the offsets into consideration in order to determine which "Special" event and corresponding name to use:
offset = 0
Iterate through dfA:
get dfA.event
get dfA.context#
Iterate through dfB:
if dfB.event == dfA.event:
dfA.newContext# = dfB.context#
offset = dfA.new_context# - dfA.context#
if dfB.event == "Special":
dfA.newContext# = dfA.context# - offset
Dataframe A
+-------------+---------+------+
|dfA.context# |dfA.event| Name |
+-------------+---------+------+
| 1465 | Bobcat | Allie|
| 1466 | Turkey |Duncan|
| 1467 | Cobra |Taylor|
| 1468 | Horse |Heath |
| 1469 | Whale | Hank |
| 1470 | Special | Emma |
| 1471 | Special | Mark |
| 1472 | Special | John |
| 1473 | Special | Terr |
| 1474 | Special | Bryl |
| 1475 | Special | Joe |
| 1476 | Special | Jamie|
| 1477 | Special | Alex |
| 1478 | Special | Sandy|
| 1479 | Special | Bruce|
| 1480 | Special | Jared|
| 1481 | Special |Hayley|
| 1482 | Special |George|
| 1483 | Special | Anna |
| 1484 | Special | Greg |
| 1485 | Special | Hans |
| 1486 | Special | Ellie|
| 1487 | Special | Tere |
| 1488 | Special | Sara |
| 1490 | Special | Mike |
| 1492 | Special | Joel |
| 1494 | Special | Tony |
| 1496 | Special | Bryce|
| 1498 | Special | Cynth|
| 1500 | Special | Kyle |
| 1501 | Special |Taylor|
| 1502 | Special | Belle|
| 1503 | Toucan | Dale |
| 1504 | Dolphin | Tim |
| 1505 | Zebra | Ham |
| 1506 | Hyena | Griff|
| 1507 | Shark |Charli|
| 1508 | Rat | Blake|
+-------------+---------+------+
Dataframe B
+-------------+---------+
|dfB.context# |dfB.event|
+-------------+---------+
| 1459 | Dog |
| 1463 | Bobcat |
| 1469 | Special |
| 1479 | Special |
| 1486 | Special |
| 1489 | Special |
| 1491 | Special |
| 1493 | Special |
| 1495 | Toucan |
| 1497 | Zebra |
| 1499 | Shark |
+-------------+---------+
Desired DF
+-------------+---------+------+---------------+
|dfA.context# |dfA.event| Name |dfA.newContext#|
+-------------+---------+------+---------------+
| 1465 | Bobcat | Allie| 1463 | (offset of 2)
| 1466 | Turkey |Duncan| |
| 1467 | Cobra |Taylor| |
| 1468 | Horse |Heath | |
| 1469 | Whale | Hank | |
| 1470 | Special | Emma | |
| 1471 | Special | Mark | 1469 | (offset of 2)
| 1472 | Special | John | |
| 1473 | Special | Terr | |
| 1474 | Special | Bryl | |
| 1475 | Special | Joe | |
| 1476 | Special | Jamie| |
| 1477 | Special | Alex | |
| 1478 | Special | Sandy| |
| 1479 | Special | Bruce| |
| 1480 | Special | Jared| |
| 1481 | Special |Hayley| 1479 | (offset of 2)
| 1482 | Special |George| |
| 1483 | Special | Anna | |
| 1484 | Special | Greg | |
| 1485 | Special | Hans | |
| 1486 | Special | Ellie| |
| 1487 | Special | Tere | |
| 1488 | Special | Sara | 1486 | (offset of 2)
| 1490 | Special | Mike | 1489 | (offset of 1)
| 1492 | Special | Joel | 1491 | (offset of 1)
| 1494 | Special | Tony | 1493 | (offset of 1)
| 1496 | Special | Bryce| |
| 1498 | Special | Cynth| |
| 1500 | Special | Kyle | |
| 1501 | Special |Taylor| |
| 1502 | Special | Belle| |
| 1503 | Toucan | Dale | 1495 | (offset of 8)
| 1504 | Dolphin | Tim | |
| 1505 | Zebra | Ham | 1497 | (offset of 8)
| 1506 | Hyena | Griff| |
| 1507 | Shark |Charli| 1499 | (offset of 8)
| 1508 | Rat | Blake| |
+-------------+---------+------+---------------+
The only way I can think of is to use the offsets but I'm not sure how to go about it other than through two loops. I'll need to refer to the previous offset in order to get the correct "Special" event. You can see for dfB.context=1489, dfA.context does not have 1491, therefore it'll default to the next available event at dfA.context=1490, mark the offset as 1 and then use the offset for the next matching. This happens until dfA.context=1503 because dfB.Event matches dfA.Event Toucan, at which point we mark an offset of 8. Basically, we match based on Event, but if there multiple of the same Event, we choose to use the previous matching's offset that we have recorded.
来源:https://stackoverflow.com/questions/63543875/how-can-i-iterate-through-two-dataframes-and-assign-a-new-values-based-on-compar