I need to merge three datasets so that I can create a custom function that would do the following: Create a function called theme_by_year that takes as input a year (as an integer) and shows the theme ids and theme names (listed in order by theme id) that were in sets that year. The column names must be id and name_themes (to differentiate between the name of a theme and the name of a set) in that order. The index should be reset and go from 0 to n-1. Each theme should only be listed once even if it appeared in more than one set from that year -- duplicate themes should be based on theme id and not name since there are some themes with the same name but with a different id. Hint: It will help if you were to think about merging appropriate DataFrames to help you get this answer. The desired output should look like this: theme_by_year(1960) Output: id    name_themes 0    371    Supplemental 1    497    Books 2    513    Classic

COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
1st Edition
ISBN:9780357392676
Author:FREUND, Steven
Publisher:FREUND, Steven
Chapter8: Working With Trendlines, Pivottables, Pivotcharts, And Slicers
Section: Chapter Questions
Problem 8EYW
icon
Related questions
icon
Concept explainers
Question

I need to merge three datasets so that I can create a custom function that would do the following:

Create a function called theme_by_year that takes as input a year (as an integer) and shows the theme ids and theme names (listed in order by theme id) that were in sets that year.

The column names must be id and name_themes (to differentiate between the name of a theme and the name of a set) in that order.

The index should be reset and go from 0 to n-1.

Each theme should only be listed once even if it appeared in more than one set from that year -- duplicate themes should be based on theme id and not name since there are some themes with the same name but with a different id.

Hint: It will help if you were to think about merging appropriate DataFrames to help you get this answer.

The desired output should look like this:

theme_by_year(1960)

Output:

id    name_themes
0    371    Supplemental
1    497    Books
2    513    Classic

Here's what I tried, but I did not get the desired output:

 

Here's what I tried, but I did not get the desired output:
merged_data = inventory_sets.merge(sets, on="set_num"). merge (themes,
on="id")
def theme_by_year(year):
# Filter for the desired year
year_data = merged_data[merged_data["year"] == year]
# Remove duplicate themes based on theme id
unique_themes = year_data.drop_duplicates (subset="id")[["id",
"name"]]
# Rename columns to id and name_themes
unique_themes = unique_themes.rename (columns={'theme_id':
'id', "name": "name_themes"})
# Reset index to 0 to n-1
unique_themes = unique_themes.reset_index (drop=True)
return unique_themes [["id", "name_themes"]]
Transcribed Image Text:Here's what I tried, but I did not get the desired output: merged_data = inventory_sets.merge(sets, on="set_num"). merge (themes, on="id") def theme_by_year(year): # Filter for the desired year year_data = merged_data[merged_data["year"] == year] # Remove duplicate themes based on theme id unique_themes = year_data.drop_duplicates (subset="id")[["id", "name"]] # Rename columns to id and name_themes unique_themes = unique_themes.rename (columns={'theme_id': 'id', "name": "name_themes"}) # Reset index to 0 to n-1 unique_themes = unique_themes.reset_index (drop=True) return unique_themes [["id", "name_themes"]]
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Query Syntax
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L