简体   繁体   中英

Pandas dataframe conditional column update based on another dataframe

I have two dataframes with two columns each - 'MeetingId' and 'TAB'. The first dataframe is the full table, but it has some errors in the 'TAB' column. The second dataframe has the solutions to the errors. I would like to replace the 'TAB' column of the first dataframe with the 'TAB' column of the second datafrmae if the 'MeetingId's match up.

Example of table:

MeetingId    TAB
123          TRUE
124          FALSE

Code:

df1 = meetingdf1
df1.set_index("MeetingId")
df2 = meetingdf2
df2.set_index("MeetingId")
df1.update(df2)
print(df1)
df['TAB'] = df.apply(lambda x: df2[df2['MeetingId'] == x['MeetingId']]['TAB'].values[0], axis=1)

OR

df.loc[df['MeetingId'].isin(df2['MeetingId']), 'TAB'] = df2['TAB']

Example:

> df

   MeetingId    TAB
0        123   True
1        124  False

> df2

   MeetingId    TAB
0        123  False
1        124   True
2        125  False

Output after running code above:

> df

   MeetingId    TAB
0        123  False
1        124   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