Pandas : Merge values to same column (replace missing values by merging dataframes)

Question:

I’m sure this has been asked before, but I can’t seem to find a solution.

Consider the following dataframes:

df1 = pd.DataFrame({
    'Letters':('A','B','C','D','E','F'),
    'Position':(1,np.nan,3,4,np.nan,6)
})

df2 = pd.DataFrame({
    'Letters':('B','E'),
    'Position':(2,5)
})

I want to replace the missing values in df1 with their corresponding value in df2. However, when I merge as follows:

df = pd.merge(df1, df2, on='Letters',how='left')

I end up with 2 Position columns as follows:

enter image description here

when I want:

enter image description here

How can I achieve this? Thank you in advance.

Asked By: brb

||

Answers:

We can use the fillna method to get the expected result :

df['position'] = df['position_x'].fillna(df['position_y'])
Answered By: tlentali
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.