Fill missing values from another Pandas dataframe with different shape
<p>I have 2 tables of different sizes which I would like to merge in the following way in Python using Pandas:</p>

<pre><code>UID Property    Date
1   A           10/02/2016
2   B           NaN
3   A           10/02/2016
4   C           NaN
5   C           NaN
6   A           10/02/2016
</code></pre>

<p>Table 1 contains information about Property transactions and a Date related to the Property. As some of the dates are NaNs, I would like to proxy them from another table (Table 2) containing information solely about properties, but not replacing any dates in Table 1:</p>

<pre><code>Property    DateProxy
A           01/01/2016
B           03/04/2016
C           16/05/2016
</code></pre>

<p>In the end I would like to obtain the following:</p>

<pre><code>UID Property    Date
1   A           10/02/2016 (kept from T1)
2   B           03/04/2016 (imported from T2)
3   A           10/02/2016 (kept from T1)
4   C           16/05/2016 (imported from T2)
5   C           16/05/2016 (imported from T2)
6   A           10/02/2016 (kept from T1)
</code></pre>

<p>First let's merge the two datasets: we don't overwrite the original date:</p>

<pre><code>df_merge = pandas.merge(T1, T2, on='Property')
</code></pre>

<p>then we replace the missing values copying them from the 'DateProxy' field:</p>

<pre><code>df_merge.Date = df_merge.apply(
    lambda x: x['Date'] + ' (kept from T1)' if x['Date'] == x['Date']
                                            else x['DateProxy'] + ' (imported from T2)',
    axis=1
)
</code></pre>

<p>(the x['Date'] == x['Date'] is to check that it isn't NaN, NaN is not equal to itself). Finally we can drop the proxy column:</p>

<pre><code>df_final = df_merge.drop('DateProxy', axis=1)
</code></pre>

python pandas merge