我有兩個資料框 - 有影響力的醫學期刊串列和來自更廣泛串列期刊的文章串列。
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
我想合并兩個資料幀并在第二個資料幀中創建一個帶有文章標題的新列,它將標記為二進制輸出,其中文章是否來自有影響力的期刊(二進制輸出)。
預期輸出
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
欣賞創意!
uj5u.com熱心網友回復:
您可以先將該值設定為 False,然后將滿足條件的那些設定為 true。
df2['influential']=0
df2['influential'][df2['Journal'].isin(df1['Journal'].values)]=1
uj5u.com熱心網友回復:
你也可以試試這個
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/370802.html
