update values in one dataframe based on another dataframe - Pandas - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: update values in one dataframe based on another dataframe - Pandas (/thread-34623.html) |
update values in one dataframe based on another dataframe - Pandas - iliasb - Aug-14-2021 Hello everyone I have a simple problem but for the life of me I can't find any straightforward answer on how to do it. So l have one dataframe with a column email and a column acronym. Another dataframe has the same columns. My goal is now to update the acronym in the first dataframe based on a match between the email in the first and the second dataframe. To visually illustrate: df1: Email - Acronym [email protected] A [email protected] Nan df2: Email - Acronym [email protected] A [email protected] B [email protected] C My goal is to update df1's Acronym on the second row, based on whats found in df2. I have been delving in the documentation of pandas for a while, to no avail. Any helpers? Thanks! RE: update values in one dataframe based on another dataframe - Pandas - iliasb - Aug-14-2021 Update: as if this forum gave me an epiphany, as soon as I posted the thread, the SQL lightbulb lit up. For the ones interested, the solution: >>> pd.merge(df1,df2,on='Email',how='left') Closed! edit: not closed, it did not do what I expected. Open! RE: update values in one dataframe based on another dataframe - Pandas - jefsummers - Aug-14-2021 Assuming that the email values in one are present in the other import pandas as pd d = {'email': ['[email protected]', '[email protected]', '[email protected]'], 'acronym': ['radar', 'laser', 'cia']} df1 = pd.DataFrame(data=d) d2 = {'email': ['[email protected]', '[email protected]', '[email protected]'], 'acronym': ['radar', 'fbi','jupyter']} df2 = pd.DataFrame(data=d2) df1['acronym'] = df2[df2['email'] == df1['email']]['acronym'] df1
|