I have two dataframes - the list of influential medical journals and the list of articles from a broader list journals.
journal_id journal_title
1 Journal 1
2 Journal 2
3 Journal 3
article_id journal_title article_title
1 Journal 1 Title 1
2 Journal 2 Title 2
3 Journal 18 Title 3
4 Journal 55 Title 4
I want to merge two dataframes and create a new column in the second dataframe with article titles, which will mark as a binary output where the article is from influential journal or not (binary output).
Expected output
article_id journal_title article_title influential
1 Journal 1 Title 1 1
2 Journal 2 Title 2 1
3 Journal 18 Title 3 0
4 Journal 55 Title 4 0
Appreciate ideas!
CodePudding user response:
You can first set the value to False, and then set for true for those who fulfill the condition.
df2['influential']=0
df2['influential'][df2['Journal'].isin(df1['Journal'].values)]=1
CodePudding user response:
You can also try this
df2 = df2.merge(df1['journal_title'], how='left', on='journal_title', indicator=True) # merges & creates indicators for matches
df2['influential'] = df2['_merge'].apply(lambda x: 1 if x == 'both' else 0) # if matches (both) then 1 else 0 for (left_only & right_only)
df2.drop(['_merge'], axis=1, inplace=True) #drops the column