Pandas: Merge two dataframe columns
<p>Consider two dataframes:</p>

<pre><code>df_a = pd.DataFrame([
        ['a', 1],
        ['b', 2],
        ['c', NaN],
    ], columns=['name', 'value'])

df_b = pd.DataFrame([
        ['a', 1],
        ['b', NaN],
        ['c', 3],
        ['d', 4]
    ], columns=['name', 'value'])
</code></pre>

<p>So looking like</p>

<pre><code># df_a
  name  value
0   a   1
1   b   2
2   c   NaN

# df_b
  name  value
0   a   1
1   b   NaN
2   c   3
3   d   4
</code></pre>

<p>I want to merge these two dataframes and fill in the NaN values of the <code>value</code> column with the existing values in the other column. In other words, I want out:</p>

<pre><code># DESIRED RESULT
  name  value
0   a   1
1   b   2
2   c   3
3   d   4
</code></pre>

<p>Sure, I can do this with a custom <code>.map</code> or <code>.apply</code>, but I want a solution that uses <code>merge</code> or the like, not writing a custom merge function. How can this be done?</p>

<p>I think you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.combine_first.html" rel="nofollow"><code>combine_first</code></a>:</p>

<pre><code>print (df_b.combine_first(df_a))
  name  value
0    a    1.0
1    b    2.0
2    c    3.0
3    d    4.0
</code></pre>

<p>Or <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html" rel="nofollow"><code>fillna</code></a>:</p>

<pre><code>print (df_b.fillna(df_a))
  name  value
0    a    1.0
1    b    2.0
2    c    3.0
3    d    4.0
</code></pre>

<p>Solution with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.update.html" rel="nofollow"><code>update</code></a> is not so common as <code>combine_first</code>:</p>

<pre><code>df_b.update(df_a)
print (df_b)
  name  value
0    a    1.0
1    b    2.0
2    c    3.0
3    d    4.0
</code></pre>

python pandas dataframe merge