Home > Net >  Pandas dataframe conditional column update based on another dataframe
Pandas dataframe conditional column update based on another dataframe

Time:11-30

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)

CodePudding user response:

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
  • Related