[英]Iterate on a pandas dataframe column and create a new column based on condition
[英]Merge two pandas dataframe and create a new binary column based on condition
我有兩個數據框 - 有影響力的醫學期刊列表和更廣泛列表期刊的文章列表。
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
我想合並兩個數據框並在第二個 dataframe 中創建一個帶有文章標題的新列,這將標記為二進制 output,其中文章是否來自有影響力的期刊(二進制輸出)。
預期 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
欣賞創意!
您可以先將值設置為False,然后將滿足條件的設置為true。
df2['influential']=0
df2['influential'][df2['Journal'].isin(df1['Journal'].values)]=1
你可以試試這個
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.