# Python Pandas For Your Grandpa - 5.1 Challenge: COVID Tracing

Contents

You track the whereabouts of 100 individuals in a DataFrame called `whereabouts`. Each person has a corresponding list of `place` ids indicating the places they’ve visited in the recent week. You also track which places have been exposed to COVID-19 in a list called `exposed`. For each person, identify the places they visited which have been exposed. Make this a new list-column in `whereabouts` called exposures.

``````import numpy as np
import pandas as pd

# exposed places
exposed = [0,5,9]

generator = np.random.default_rng(2468)
Nplaces = 10
Npersons = 10
place_ids = np.arange(Nplaces)
visits = generator.choice(place_ids, size=3*Nplaces, replace=True)
split_idxs = np.sort(generator.choice(len(visits), size=9, replace=True))
'person_id': range(Npersons),
'places': [np.unique(x).tolist() for x in np.array_split(visits, split_idxs)]
})
##    person_id              places
## 0          0        [3, 4, 5, 6]
## 1          1                  []
## 2          2                 [3]
## 3          3           [6, 8, 9]
## 4          4                 [3]
## 5          5  [0, 2, 5, 6, 7, 8]
## 6          6              [2, 7]
## 7          7        [0, 5, 8, 9]
## 8          8           [2, 7, 9]
## 9          9        [0, 5, 8, 9]
``````

## Solution 1

``````whereabouts['exposures'] = whereabouts['places'].apply(lambda x: list(set(x) & set(exposed)))
##    person_id              places  exposures
## 0          0        [3, 4, 5, 6]        [5]
## 1          1                  []         []
## 2          2                 [3]         []
## 3          3           [6, 8, 9]        [9]
## 4          4                 [3]         []
## 5          5  [0, 2, 5, 6, 7, 8]     [0, 5]
## 6          6              [2, 7]         []
## 7          7        [0, 5, 8, 9]  [0, 9, 5]
## 8          8           [2, 7, 9]        [9]
## 9          9        [0, 5, 8, 9]  [0, 9, 5]
``````

## Solution 2

``````expanded = whereabouts.explode(column='places')
filtered = expanded.loc[expanded.places.isin(exposed)]
aggregated = filtered.groupby('person_id')[['places']].agg(list)
aggregated.rename(columns={'places':'exposures'}, inplace=True)