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:
when I want:
How can I achieve this? Thank you in advance.
Answers:
We can use the fillna
method to get the expected result :
df['position'] = df['position_x'].fillna(df['position_y'])
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:
when I want:
How can I achieve this? Thank you in advance.
We can use the fillna
method to get the expected result :
df['position'] = df['position_x'].fillna(df['position_y'])