Abhinab k
Abhinab k

Reputation: 21

How to use the ids from one dataframe column to get the associated value from another dataframe?

I have 2 api calls. One gives me the titles of all the popular movies and have stored it in a dataframe (df_popular_titles).

{'adult': {0: False, 1: False, 2: False, 3: False, 4: False},
 'genre_ids': {0: [12, 28, 878],
  1: [10751, 16, 12, 35, 14],
  2: [28, 18],
  3: [16, 878, 12, 28, 10751],
  4: [28, 12, 14]},
 'original_language': {0: 'en', 1: 'en', 2: 'en', 3: 'en', 4: 'en'},
 'overview': {0: 'Four years after Isla Nublar was destroyed, dinosaurs now live—and hunt—alongside humans all over the world. This fragile balance will reshape the future and determine, once and for all, whether human beings are to remain the apex predators on a planet they now share with history’s most fearsome creatures.',
  1: 'A fanboy of a supervillain supergroup known as the Vicious 6, Gru hatches a plan to become evil enough to join them, with the backup of his followers, the Minions.',
  2: 'After more than thirty years of service as one of the Navy’s top aviators, and dodging the advancement in rank that would ground him, Pete “Maverick” Mitchell finds himself training a detachment of TOP GUN graduates for a specialized mission the likes of which no living pilot has ever seen.',
  3: 'Legendary Space Ranger Buzz Lightyear embarks on an intergalactic adventure alongside a group of ambitious recruits and his robot companion Sox.',
  4: 'After his retirement is interrupted by Gorr the God Butcher, a galactic killer who seeks the extinction of the gods, Thor enlists the help of King Valkyrie, Korg, and ex-girlfriend Jane Foster, who now inexplicably wields Mjolnir as the Mighty Thor. Together they embark upon a harrowing cosmic adventure to uncover the mystery of the God Butcher’s vengeance and stop him before it’s too late.'},
 'popularity': {0: 10436.917,
  1: 8821.801,
  2: 7567.017,
  3: 5255.93,
  4: 8772.151},
 'release_date': {0: '2022-06-01',
  1: '2022-06-29',
  2: '2022-05-24',
  3: '2022-06-15',
  4: '2022-07-06'},
 'title': {0: 'Jurassic World Dominion',
  1: 'Minions: The Rise of Gru',
  2: 'Top Gun: Maverick',
  3: 'Lightyear',
  4: 'Thor: Love and Thunder'},
 'vote_average': {0: 7.0, 1: 7.5, 2: 8.4, 3: 7.3, 4: 6.7},
 'vote_count': {0: 2054, 1: 489, 2: 1690, 3: 987, 4: 1317}}

The other api call gives the genre names associated with genre_ids and have stored it a dataframe (df_genre).

{'id': {0: 28.0, 1: 12.0, 2: 16.0, 3: 35.0, 4: 80.0},
 'name': {0: 'Action',
  1: 'Adventure',
  2: 'Animation',
  3: 'Comedy',
  4: 'Crime'}}

How can i change the genre_ids in df_popular_titles to names associated with the genre id using the df_genre dataframe?

Upvotes: 0

Views: 43

Answers (1)

Lorenzo Bonetti
Lorenzo Bonetti

Reputation: 590

You can filter the genres names by using isin:

df_popular_titles['name'] = df_popular_titles['genre_ids'].apply(lambda x: df_genre[df_genre['id'].isin(x)]['name'].tolist())

Upvotes: 1

Related Questions