Below is the code for the function `minifigs_from_themes`:
```python
def minifigs_from_themes(theme_id):
# Filter the sets DataFrame by theme_id
sets_theme = sets[sets['theme_id'] == theme_id]
# Merge sets_theme with inventory_parts to get the minifigs
minifigs = pd.merge(sets_theme, inventory_parts[['inventory_id', 'part_num']], left_on='set_num', right_on='inventory_id', how='inner')
# Merge minifigs with parts to get the minifig names
minifigs = pd.merge(minifigs, parts[['part_num', 'name']], on='part_num', how='left')
# Drop duplicate minifigs
minifigs = minifigs.drop_duplicates(subset='fig_num')
# Reset the index
minifigs.reset_index(drop=True, inplace=True)
# Rename the columns
minifigs.rename(columns={'fig_num': 'fig_num', 'name': 'fig_name'}, inplace=True)
return minifigs[['fig_num', 'fig_name']]
```
To call the `minifigs_from_themes` function with the theme number of the `Millennium Falcon Prep Exercise`, you can use the following code:
```python
minifigs_from_themes(158)
```
This should return a DataFrame with 149 minifigures for the respective theme.
**Q16:**
- Create a function called `minifigs_from_themes` that takes as input a theme id (as an integer) and outputs all the minifigs contained in sets with that theme.
- The DataFrame should be in `fig_num` order with the following columns/column names: `fig_num`, `fig_name`.
- The index should go from 0 to n-1.
- Exact duplicates should be removed so that only unique figs are included.
**Code Check:** Call the `minifigs_from_themes()` function using the theme number of the set discussed in the `Millennium Falcon Prep Exercise`. There should be 149 minifigs output for that respective theme.
1 answer