Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace values with values from other dataframe

Tags:

python

pandas

I have two dataframes:

df1:

enter image description here

df2:

enter image description here

I'd like to update df2 with df1 values to create:

enter image description here

Code to generate example dataframes below:

import pandas as pd

test_dict = {'Customer': ['A', 'B', 'C'], 'Last Accessed': ['7/16/2020','7/5/2020', '7/1/2020']}
df1 = pd.DataFrame.from_dict(test_dict)

test_dict = {'Customer': ['A', 'B', 'C', 'D', 'E', 'F'], 'Date Accessed': ['5/15/2020','5/15/2020', '5/15/2020', '5/15/2020', '5/15/2020', '5/15/2020']}
df2 = pd.DataFrame.from_dict(test_dict)
like image 475
Brandon Avatar asked Jan 30 '26 10:01

Brandon


1 Answers

Let us try concat then drop_duplicates

df = pd.concat([df1.rename(columns={'Last Accessed':'Date Accessed'}),df2]).drop_duplicates('Customer')
Out[81]: 
  Customer Date Accessed
0        A     7/16/2020
1        B      7/5/2020
2        C      7/1/2020
3        D     5/15/2020
4        E     5/15/2020
5        F     5/15/2020
like image 185
BENY Avatar answered Jan 31 '26 23:01

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!