Search code examples
pythonpandasdataframelist

How to change columns values(list) using another data frame in Python


I have two data frame, I need to change column values of first data frame that are in list, using second data frame.

df1 = pd.DataFrame({'title':['The Godfather','Fight Club','The Empire'], 'genre_ids':[[18, 80],[18],[12, 28, 878]]})

    title           genre_ids
0   The Godfather   [18, 80]
1   Fight Club      [18]
2   The Empire      [12, 28, 878]


df2 = pd.DataFrame({'id':[18,80,12,28,878,99],'name':['Action','Adventure','Adventure','Animation','Comedy','Documentary']})


    id  name
0   18  Action
1   80  Horror
2   12  Adventure
3   28  Animation
4   878 Comedy
5   99  Documentary

How can I assign genere_ids like this using df2 in python

       title       genre_ids  
0   The Godfather  [Action, Horror] 
1   Fight Club     [Action]
2   The Empire     [Adventure, Animation, Comedy]

Solution

  • You can do (note: id 80 is Adventure in your example, not Horror):

    m = dict(zip(df2["id"], df2["name"]))
    df1["genre_ids"] = df1["genre_ids"].apply(lambda l: [m.get(v) for v in l])
    
    print(df1)
    

    Prints:

               title                       genre_ids
    0  The Godfather             [Action, Adventure]
    1     Fight Club                        [Action]
    2     The Empire  [Adventure, Animation, Comedy]