-1

i turned a json file into a dataframe, but I am unsure of how to map a certain value from the json dataframe onto the existing data frame i have.

df1 =     # (2nd column does'nt matter just there)
category_id tags
1 a
1 a
10 b
10 c
40 d
df2(json) =
id title
1 film
2 music
3 travel
4 cooking
5 dance

I would like to make a new column in df1, that maps the titles from the df2 onto df1 corresponding to the category_id. I am sorry I am new to python programming. I know I can hard code the dictionary and key values and go from there. However I was wondering if there is a way with python/pandas to do this in an easier way.

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41

1 Answers1

0

You can use pandas.Series.map() which maps values of Series according to input correspondence.

df1['tilte'] = df1['category_id'].map(df2.set_index('id')['title'])
# print(df1)

   category_id tags tilte
0            1    a  film
1            1    a  film
2           10    b   NaN
3           10    c   NaN
4           40    d   NaN
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52