Python Forum
update values in one dataframe based on another dataframe - Pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
update values in one dataframe based on another dataframe - Pandas
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:

Email - Acronym
[email protected] A
[email protected] Nan

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!
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')


edit: not closed, it did not do what I expected. Open!
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'] 
email acronym 0 [email protected] radar 1 [email protected] fbi 2 [email protected] jupyter

Possibly Related Threads…
Thread Author Replies Views Last Post
  Describe repeating sequences in Dataframe and plot results PBRM 0 50 Yesterday, 10:29 PM
Last Post: PBRM
  New Dataframe Column Based on Several Conditions nb1214 1 192 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  pandas: Compute the % of the unique values in a column JaneTan 1 311 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Putting column name to dataframe, can't work. jonah88888 1 393 Sep-28-2021, 07:45 PM
Last Post: deanhystad
  Remove specific values from dataframe jonah88888 0 337 Sep-24-2021, 05:09 AM
Last Post: jonah88888
  Checking for a recognized text in a Dataframe KDE 0 426 Aug-31-2021, 11:19 PM
Last Post: KDE
  Pandas Data frame column condition check based on length of the value aditi06 1 669 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 783 Jun-22-2021, 07:57 PM
Last Post: snippsat
  Setting the x-axis to a specific column in a dataframe devansing 0 727 May-23-2021, 12:11 AM
Last Post: devansing
  Basic help with a dataframe stylingpat 2 1,147 May-21-2021, 06:52 PM
Last Post: Caprone

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020