On start I have two DataFrames and one variable:
id=1
df1 = pd.DataFrame({'id': [1, 2], 'col0': [3, 4]})
df2 = pd.DataFrame({'col1': [13, 14, 15],'col2': [23, 24, 25]})
I have to map id variable and the corresponding col0 cell from df1 DataFrame to all rows in df2 DataFrame. I tryed and as the result I made the code below:
df2.insert(0, "id", id)
df2.insert(1, "col0", df1[df1['id']==id]['col0'])
It seems to me that the code should work correctly, but unfortunatelly I have a NaN value in the col0 column.
id col0 col1 col2
0 1 3.0 13 23
1 1 NaN 14 24
2 1 NaN 15 25
The expected result was:
id col0 col1 col2
0 1 3.0 13 23
1 1 3.0 14 24
2 1 3.0 15 25
I've spent over an hour and can't figure out why I'm getting this kind of result. If possible, could you, please:
- explain briefly why I am getting the error
- fix my mistake in the code

NaN's? Could you please post the expected result?