To solve this task, I will perform the following steps:
1. Create an empty list called `sets` to store the sets a minifig has appeared in.
2. Iterate over each row in the `sets` DataFrame.
3. Check if the figure number in the current row matches the input minifig number.
4. If there is a match, append the set name to the `sets` list.
5. Create a new DataFrame called `result` using the `sets` list.
6. Sort the `result` DataFrame first by `set_num` and then by `version`.
7. Reset the index of the `result` DataFrame.
8. Return the `result` DataFrame.
```python
def sets_from_minifig(minifig_num):
sets = []
for _, row in sets.iterrows():
if row['fig_num'] == minifig_num:
sets.append(row['name_sets'])
result = pd.DataFrame(sets, columns=['set_num', 'version', 'name_sets', 'fig_num', 'fig_name'])
result.sort_values(['set_num', 'version'], inplace=True)
result.reset_index(drop=True, inplace=True)
return result
# Call the function on the 'fig-001039' minifig
minifig_sets = sets_from_minifig('fig-001039')
minifig_sets
```
**Note:** Make sure to replace `sets` with the actual DataFrame variable name in your code.
**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