2

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!

Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
  • You could use the `.isin()` method. This seems to be the same problem: [link](https://stackoverflow.com/questions/50449088/check-if-value-from-one-dataframe-exists-in-another-dataframe/50449144) – Yehla Dec 01 '21 at 12:50

2 Answers2

3

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
Eha
  • 46
  • 2
-1

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
Ashok Thakur
  • 116
  • 5
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '21 at 15:04