Home > Software design >  I am trying to change the numbers in the genre_ids column to the actual names. I have tried the foll
I am trying to change the numbers in the genre_ids column to the actual names. I have tried the foll

Time:06-08

last code I tried and output...

For example, in the genre_ids column I would like the number "28" to say "action" and the number "12" to say "adventure" and so on.

CodePudding user response:

dict_map = {28 : 'action', 12: 'adventure', ...}

tmdb_movie_info_clean['genre_ids'] = tmdb_movie_info_clean['genre_ids'].apply(lambda x: [dict_map[i] for i in x])

If the values in 'genre_ids' are string then use:

dict_map = {'28' : 'action', '12': 'adventure', ...}
tmdb_movie_info_clean['genre_ids'] = tmdb_movie_info_clean['genre_ids'].apply(lambda x: [dict_map[i] for i in x])

CodePudding user response:


I'm assuming that 'genre_ids' are numbers and not Strings. Have you tried to remove the '' around all of the keys inside the replace function like so?
tmpdb_movie_invo_clean['genre_ids'].replace({28:'action', ...}, inplace=TRUE)

CodePudding user response:

Here I have used the for loop where it check the each list each element if its exists in provided real name directory, it will replace the value by name: Code:

data = {
       "genre_ids": [[28, 14, 10751],[14, 12, 16, 10751]]
        }

#load data into a DataFrame object:
df = pd.DataFrame(data)
replace = {'28' : 'action',
          '12': 'adventure'}
for i in range(len(df)):
    for j in range(len(df['genre_ids'][i])):
        if str(df['genre_ids'][i][j]) in replace:
            df['genre_ids'][i][j] = replace[str(df['genre_ids'][i][j])]

df

Output:

                    genre_ids
0   [action, 14, 10751]
1   [14, adventure, 16, 10751]
  • Related