-1

I have a dictionary that looks like this :

{'Andrew':['Photography'],'Peter':['Travel']}

I also have a db that looks like this:

ID      Hobby
1       Travel
2       Photography
3       Football
..........

I would like to add a thirth column to the Dataframe to achieve:

ID     Hobby        Name
1      Travel       Peter
2      Photography  Andrew
3      Football     NaN

I did find this Adding a new pandas column with mapped value from a dictionary, but I do not need the column to contain the value from the dictionary, but the key. Thanks

Community
  • 1
  • 1
Andrei Cozma
  • 950
  • 3
  • 9
  • 14

1 Answers1

0

You can try something like that:

import pandas as pd
equiv = {'Andrew':['Photography'],'Peter':['Travel'], 'Jack': ['Football']}

df = pd.DataFrame( {'Hobby':[equiv[x][0] for x in equiv], 'Name': [key for key in equiv.keys()]})
print(df)
DimKoim
  • 1,024
  • 6
  • 20
  • 33