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
  Assigning conditional values in Pandas Scott 3 790 Dec-19-2023, 03:10 AM
Last Post: Larz60+
  How to most effectively unpack list of name-value pair dictionaries in a dataframe? zlim 1 646 Nov-07-2023, 10:56 PM
Last Post: zlim
  How to add columns to polars dataframe sayyedkamran 1 1,756 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 837 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 1,017 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  dataframe logic issues mbrown009 5 998 Sep-14-2023, 02:48 AM
Last Post: deanhystad
  attempt to split values from within a dataframe column mbrown009 8 2,326 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,111 Jan-06-2023, 10:53 PM
Last Post: haihal
  Validating Dataframe Using Second Dataframe tdbozarth 0 758 Dec-05-2022, 06:03 PM
Last Post: tdbozarth
  How to insert data in a dataframe? man0s 1 1,323 Apr-26-2022, 11:36 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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