Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df1 = meetingdf1
df1.set_index("MeetingId")
df2 = meetingdf2
df2.set_index("MeetingId")
df1.update(df2)
print(df1)

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading