Firstly we can import some packages which might be useful
import pandas as pd
import datetime
Say I now have a dataframe which has a date, name and age column.
df1 = pd.DataFrame({'date': ['10-04-2020', '04-07-2019', '12-05-2015' ], 'name': ['john', 'tim', 'sam'], 'age':[20, 22, 27]})
Now say I have another dataframe with some random columns
df2 = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
Question:
How can I take the age value in df1 filtered on the date (can select this value) and populate a whole new column in df2 with this value? Ideally this method should generalise for any number of rows in the dataframe.
Tried
The following is what I have tried (on a similar example) but for some reason it doesn't seem to work (it just shows nan values in the majority of column entries except for a few which randomly seem to populate).
y = datetime.datetime(2015, 5, 12)
df2['new'] = df1[(df1['date'] == y)].age
Expected Output
Since I have filtered above based on sams age (date corresponds to the row with sams name) I would like the new column to be added to df2 with his age as all the entries (in this case 27 repeated 3 times).
df2 = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'new': [27, 27, 27]})
samone with age27and the other with25?df1) and then populate a new column (indf2) with that value