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:
offset = 0
Iterate through dfA:
extract event
extract 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 |
+-------------+---------+------+
| 0 | Special | Bob |
| 2 | Special | Joan |
| 4 | Bird | Susie|
| 5 | Special | Alice|
| 6 | Special | Tom |
| 7 | Special | Luis |
| 8 | Parrot | Jill |
| 9 | Special | Reed |
| 10 | Special | Lucas|
| 11 | Snake | Kat |
| 12 | Special | Bill |
| 13 | Special | Leo |
| 14 | Special | Peter|
| 15 | Special | Mark |
| 16 | Special | Joe |
| 17 | Special | Lora |
| 18 | Special | Care |
| 19 |Elephant | David|
| 20 | Special | Ann |
| 21 | Special | Larry|
| 22 | Skunk | Tony |
+-------------+---------+------+
Dataframe B
+-------------+---------+
|dfB.context# |dfB.event|
+-------------+---------+
| 0 | Special |
| 0 | Special |
| 0 | Special |
| 1 | Special |
| 1 | Special |
| 1 | Special |
| 1 | Special |
| 2 | Bird |
| 2 | Bird |
| 3 | Special |
| 6 | Parrot |
| 6 | Parrot |
| 6 | Parrot |
| 6 | Parrot |
| 7 | Special |
| 7 | Special |
| 9 | Snake |
| 9 | Snake |
| 9 | Snake |
| 10 | Special |
| 17 |Elephant |
| 17 |Elephant |
| 17 |Elephant |
| 18 | Special |
| 18 | Special |
| 20 | Skunk |
| 20 | Skunk |
| 21 | Special |
| 26 | Antelope|
+-------------+---------+
Desired DF
+-------------+---------+------+-------------+
|dfA.context# |dfA.event| Name |dfA.newContext#|
+-------------+---------+------+-------------+
| 0 | Special | Bob | 0 |
| 2 | Special | Joan | 1 |
| 4 | Bird | Susie| 2 |
| 5 | Special | Alice| 3 |
| 6 | Special | Tom | |
| 7 | Special | Luis | |
| 8 | Parrot | Jill | 6 |
| 9 | Special | Reed | 7 |
| 10 | Special | Lucas| |
| 11 | Snake | Kat | 9 |
| 12 | Special | Bill | 10 |
| 13 | Special | Leo | |
| 14 | Special | Peter| |
| 15 | Special | Mark | |
| 16 | Special | Joe | |
| 17 | Special | Lora | |
| 18 | Special | Care | |
| 19 |Elephant | David| 17 |
| 20 | Special | Ann | 18 |
| 21 | Special | Larry| |
| 22 | Skunk | Tony | 20 |
+-------------+---------+------+-------------+
How can I iterate through the two dataframes at once and access the information ?