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
#1
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!
Reply
#2
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!
Reply
#3
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
Output:
email acronym 0 [email protected] radar 1 [email protected] fbi 2 [email protected] jupyter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create dataframe from the unique data of two dataframes Calab 6 729 Mar-02-2025, 01:51 PM
Last Post: Pedroski55
Question [Solved] Formatting cells of a pandas dataframe into an OpenDocument ods spreadsheet Calab 1 485 Mar-01-2025, 04:51 AM
Last Post: Calab
  Help Refining DataFrame GMAlves 2 1,152 Nov-05-2024, 08:51 PM
Last Post: deanhystad
  Find duplicates in a pandas dataframe list column on other rows Calab 2 1,909 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,535 Aug-26-2024, 04:52 PM
Last Post: Calab
  Loop over dataframe to fill in missing rows Scott 9 3,455 Jul-12-2024, 05:54 AM
Last Post: Scott
  Create new column in dataframe Scott 10 3,342 Jun-30-2024, 10:18 PM
Last Post: Scott
  attempt to split values from within a dataframe column mbrown009 9 5,708 Jun-20-2024, 07:59 PM
Last Post: AdamHensley
  Putting column name to dataframe, can't work. jonah88888 2 3,205 Jun-18-2024, 09:19 PM
Last Post: AdamHensley
  Add NER output to pandas dataframe dg3000 0 1,111 Apr-22-2024, 08:14 PM
Last Post: dg3000

Forum Jump:

User Panel Messages

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