I have two dataframes like this:
df1
Win ID
1 345
0 678
0 901
1 124
df2
Code ID Win
a 123 1
b nan nan
c nan nan
I want to create a new column in df1 called 'code' only where 'Win' in df1 equals '1' and insert the value from df2 column 'code' WHERE 'ID' and 'Win' in df2 are nan.
I also need to update df2 columns 'ID' and 'Win' with the 'ID' from df1 only for the 'Code' that has been assigned in df1. 'Win' should equal 1.
How would I do this?
df1 and df2 updated should look like this:
df1
Win ID Code
1 345 b
0 678 nan
0 901 nan
1 124 c
df2
Code ID Win
a 123 1
b 345 1
c 124 1
I tried to extract the index and apply values but I can not get past how to only select the value of the integer like so:
idx = df2.loc[(df2['Win'].isnull()) & (df2['ID'].isnull())].index.values.astype(int)[0]