title: Python Pandas fill missing column with left join
body: <p>I have following two data-frames.</p>
  <p>df_1</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>AA</th>
  <th>BB</th>
  <th>CC</th>
  <th>DD</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>XYZ1</td>
  <td>XYZ2</td>
  <td></td>
  </tr>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>PQR1</td>
  <td>PQR2</td>
  <td></td>
  </tr>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>XYZ4</td>
  <td>PRR9</td>
  <td></td>
  </tr>
  <tr>
  <td>&quot;Banana&quot;</td>
  <td>XYZ1</td>
  <td></td>
  <td>416</td>
  </tr>
  <tr>
  <td>&quot;Banana&quot;</td>
  <td>XYZ1</td>
  <td></td>
  <td>416</td>
  </tr>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>XYZ4</td>
  <td>PRR9</td>
  <td></td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>df_lookup</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>AA</th>
  <th>XX</th>
  <th>YY</th>
  <th>ZZ</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>XYZ1</td>
  <td>XYZ2</td>
  <td>429</td>
  </tr>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>XYZ4</td>
  <td>PRR9</td>
  <td>97</td>
  </tr>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>PQR1</td>
  <td>PQR2</td>
  <td>108</td>
  </tr>
  <tr>
  <td>&quot;Banana&quot;</td>
  <td>XYZ1</td>
  <td>PQR1</td>
  <td>416</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p><strong>Expected result:</strong></p>
  <p>My objective is to fill out the empty values in df_1. In other words:</p>
  <pre><code>if AA == &quot;Apple&quot; then 
   df_1.DD = SELECT df_lookup.ZZ 
   FROM df_lookup 
   LFET JOIN df_1 
   ON df_1.BB = df_lookup.XX, df_1.CC = df_lookup.YY
  </code></pre>
  <p>on the contrary ...</p>
  <pre><code>if AA == &quot;Banana&quot; then 
   df_1.CC = SELECT df_lookup.YY 
   FROM df_lookup 
   LFET JOIN df_1 
   ON df_1.BB = df_lookup.XX, df_1.DD = df_lookup.ZZ
  </code></pre>
  <p>df_1 (filled/modified)</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>AA</th>
  <th>BB</th>
  <th>CC</th>
  <th>DD</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>XYZ1</td>
  <td>XYZ2</td>
  <td>429</td>
  </tr>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>PQR1</td>
  <td>PQR2</td>
  <td>108</td>
  </tr>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>XYZ4</td>
  <td>PRR9</td>
  <td>97</td>
  </tr>
  <tr>
  <td>&quot;Banana&quot;</td>
  <td>XYZ1</td>
  <td>PQR1</td>
  <td>416</td>
  </tr>
  <tr>
  <td>&quot;Banana&quot;</td>
  <td>XYZ1</td>
  <td>PQR1</td>
  <td>416</td>
  </tr>
  <tr>
  <td>&quot;Apple&quot;</td>
  <td>XYZ4</td>
  <td>PRR9</td>
  <td>97</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I tried the following so far</p>
  <pre><code>apple_merged = pd.merged(df_1, df_lookup, left_on = [&quot;BB&quot;, &quot;CC&quot;], right_on = [&quot;XX&quot;, &quot;YY&quot;])
  df_1[(df_1[&quot;AA&quot;] == &quot;Apple&quot;)][&quot;DD&quot;] = apple_merged[(apple_merged.AA == &quot;Apple&quot;)][&quot;ZZ&quot;].values
  </code></pre>
  <p>I got the following error in my actual code:</p>
  <blockquote>
  <p>ValueError: Length of values (501) does not match length of index
  (602)</p>
  </blockquote>
  <p>Which seems to suggest that the shape of the data is different on the opposite side of the assignment, 501 v/s 602. But if I really did left join, should the row counts not be the same for me in this case?</p>