I have data in the following format:
| ID | Age | Sex |
|---|---|---|
| 1 | 29 | M |
| 2 | 32 | F |
| 3 | 18 | F |
| 4 | 89 | M |
| 5 | 45 | M |
and;
| ID | subID | Type | Status | Year |
|---|---|---|---|---|
| 1 | 3 | Car | Y | |
| 1 | 11 | Toyota | NULL | 2011 |
| 1 | 23 | Kia | NULL | 2009 |
| 2 | 5 | Car | N | |
| 3 | 2 | Car | Y | |
| 3 | 4 | Honda | NULL | 2019 |
| 3 | 7 | Fiat | NULL | 2006 |
| 3 | 8 | Mitsubishi | NULL | 2020 |
| 4 | 1 | Car | N | |
| 5 | 7 | Car | Y |
Each ID in the second table has a row specifying if they have a car, and additional rows stating the brand of car/s they own. Each person has a maximum of 3 cars. I want to simplify this data into a single table as so.
| ID | Age | Sex | Car? | Car.1 | Car1.year | Car.2 | Car2.year | Car.3 | Car3.year |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 29 | M | Y | Toyota | 2011 | Kia | 2009 | NULL | NULL |
| 2 | 32 | F | N | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | 18 | F | Y | Honda | 2019 | Fiat | 2006 | Mitsubishi | 2020 |
| 4 | 89 | M | N | NULL | NULL | NULL | NULL | NULL | NULL |
| 5 | 45 | M | Y | NULL | NULL | NULL | NULL | NULL | NULL |
I've tried using the mutate function in dplyr with the case_when function, but I can't check conditions in another dataframe. If I try to join the tables together, I would have multiple rows for each ID which I want to avoid. The non-standard set up of the second table makes things complicated. My only remaining idea is to switch to Python/Pandas and create a for loop that slowly loops through each ID, searches the second dataframe if the person has a car and the car brands, then mutates a column in the first dataframe. But given the size of my dataset, this would be inefficient and take a long time.
What is the best way to do this?