简体   繁体   中英

Merge two pandas dataframe and create a new binary column based on condition

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!

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

You can try this

df2 = df2.merge(df1['journal_title'], how='left', on='journal_title', indicator=True)
df2['influential'] = df2['_merge'].apply(lambda x: 1 if x == 'both' else 0)
df2.drop(['_merge'], axis=1, inplace=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM