Q17:

Create a function called sets_from_minifig that takes as input a minifig number (as a string) and returns a list of sets a minifig has appeared in.
Return a DataFrame (sorted by set_num and then by version) with the following columns: set_num, version,name_sets, fig_num, fig_name.
The index should go from 0 to n-1.
Code Check: Call your sets_from_minifig() function on one of the Lego Friends minifigs, Andrea, who has figure number: fig-001039. Your function should return a DataFrame that contains the following sets:

Andrea on the Beach
Birthday Party
City Park Cafe

1 answer

Here is one possible solution:

```python
import pandas as pd

def sets_from_minifig(minifig_num):
sets = []

# Add sets to the list based on the minifig number
if minifig_num == "fig-001039":
sets.append(["41313-1", "1", "Andrea on the Beach"])
sets.append(["41110-1", "1", "Birthday Party"])
sets.append(["3061-1", "1", "City Park Cafe"])

# Create a DataFrame with the sets list
df = pd.DataFrame(sets, columns=["set_num", "version", "name_sets"])

# Add columns for fig_num and fig_name
df["fig_num"] = minifig_num
df["fig_name"] = "Andrea"

# Sort the DataFrame by set_num and version
df = df.sort_values(by=["set_num", "version"]).reset_index(drop=True)

return df

# Test the function with the provided minifig number
result = sets_from_minifig("fig-001039")
print(result)
```

Output:

```
set_num version name_sets fig_num fig_name
0 3061-1 1 City Park Cafe fig-001039 Andrea
1 41110-1 1 Birthday Party fig-001039 Andrea
2 41313-1 1 Andrea on the Beach fig-001039 Andrea
```