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!

1
  • You could use the .isin() method. This seems to be the same problem: link Commented Dec 1, 2021 at 12:50

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

-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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.