简体   繁体   中英

Update column of a dataframe when key matches from another dataframe in pandas

I have two dataframes.

在此处输入图像描述

For all rows in df1, find the corresponding row in df2 (through matching key) and update the final column in df2 to 1. How shall I proceed in pandas?

Remove column final , use left join with indicator parameter, so is possible create 1,0 column by mapping True, False by compare both :

df = df2.drop('final', axis=1).merge(df1, how='left', indicator='final')
df['final'] = df['final'].eq('both').astype(int)

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