Jan-01-2021, 10:14 PM
I'm very new to python and really don't know where to start doing the following:
I have two dataframes, df1 and df2.
I want to map 'name' values from df2 to new columns in df1.
There are two conditions to be met, first that the 'uid' in df2 must be the same as the 'id' in df1,
and the second that the 'ndate' in df2 must be before (less than) the 'date' in df1.
Ideally each matched value should return to new columns to the right of date in df1.
Hope this makes sense, I've simplified the tables as much as possible, and this is the gist of what I'm looking to do. Any suggestions much appreciated, thanks in advance!
I have two dataframes, df1 and df2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
df1 fruit id date 0 apple 2 01 / 10 / 20 1 pear 1 15 / 09 / 20 2 banana 3 01 / 06 / 20 3 peach 4 10 / 04 / 20 df2 name uid ndate 0 paul 2 02 / 11 / 20 1 tracy 1 15 / 12 / 20 2 iain 3 01 / 05 / 20 3 frida 4 23 / 02 / 20 4 david 2 06 / 06 / 20 5 peter 3 19 / 11 / 20 6 adam 4 07 / 03 / 20 7 eve 1 30 / 11 / 20 8 hannah 2 25 / 09 / 20 9 janine 2 13 / 08 / 20 10 charlotte 5 10 / 04 / 20 |
There are two conditions to be met, first that the 'uid' in df2 must be the same as the 'id' in df1,
and the second that the 'ndate' in df2 must be before (less than) the 'date' in df1.
Ideally each matched value should return to new columns to the right of date in df1.
1 2 3 4 5 6 |
df1 fruit id date match1 match2 match3 ...(etc) 0 apple 2 01 / 10 / 20 david hannah janine 1 pear 1 15 / 09 / 20 2 banana 3 01 / 06 / 20 iain 3 peach 4 10 / 04 / 20 frida adam |