i want to use df2 as legend to my primary df ('main_df')
what "code" need to be ?
main_df = pd.DataFrame({'genre_NAME': ['comedy', 'action', 'horror'], 'genre_id': [nan, nan, nan]})
df2 = pd.DataFrame({'genre': ['comedy', 'horror'], 'id': [0, 1]})
#main_df is not relate to df2
some code
#result
print(main_df)
*OUTPUT =* 'genre_NAME': ['comedy', 'action', 'horror'], 'genre_id': [0, nan,1]
CodePudding user response:
You can access and change a specific value in a dataframe with df.loc[]. So in your case:
for _, line in df2.iterrows():
main_df.loc[main_df.genre_NAME == line.genre, "genre_id"] = line.id
