**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.

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.

To create the `minifigs_from_themes` function, you can follow these steps:

1. Start by importing the necessary libraries:
```python
import pandas as pd
```

2. Define the `minifigs_from_themes` function, which takes the theme id (as an integer) as input:
```python
def minifigs_from_themes(theme_id):
```

3. Load the `sets.csv` dataset into a DataFrame:
```python
sets = pd.read_csv('sets.csv')
```

4. Filter the DataFrame to include only rows with the given theme id:
```python
theme_sets = sets[sets['theme_id'] == theme_id]
```

5. Merge the `sets` and `inventories` DataFrames on the `set_num` columns:
```python
merged_df = pd.merge(theme_sets, inventories, on='set_num')
```

6. Merge the `merged_df` DataFrame with the `minifigs` DataFrame on the `inventory_id` columns:
```python
merged_df = pd.merge(merged_df, minifigs, on='inventory_id')
```

7. Drop duplicates from the DataFrame, keeping only unique minifigs:
```python
merged_df = merged_df.drop_duplicates(subset=['fig_num'])
```

8. Sort the DataFrame by `fig_num`:
```python
merged_df = merged_df.sort_values('fig_num')
```

9. Reset the index of the DataFrame:
```python
merged_df = merged_df.reset_index(drop=True)
```

10. Return the resulting DataFrame:
```python
return merged_df[['fig_num', 'fig_name']]
```

Here is the complete code for the `minifigs_from_themes` function:
```python
import pandas as pd

def minifigs_from_themes(theme_id):
sets = pd.read_csv('sets.csv')
theme_sets = sets[sets['theme_id'] == theme_id]
merged_df = pd.merge(theme_sets, inventories, on='set_num')
merged_df = pd.merge(merged_df, minifigs, on='inventory_id')
merged_df = merged_df.drop_duplicates(subset=['fig_num'])
merged_df = merged_df.sort_values('fig_num')
merged_df = merged_df.reset_index(drop=True)
return merged_df[['fig_num', 'fig_name']]
```

To check if the function returns the expected result, you can call it with the theme number of the set discussed in the `Millennium Falcon Prep Exercise`. Assuming the theme number of that set is `1234`, you can use the following code:
```python
minifigs_from_themes(1234)
```

If the theme number of the set is different, replace `1234` with the correct theme number. The function should return a DataFrame with `fig_num` and `fig_name` columns, containing the 149 minifigs for that respective theme.