title: Efficiently merge a dataframe with another by selecting the second dataframe based on each row
body: <p>I have a one pandas DataFrame like this,</p>
  <pre><code>    A   B  C
  0  A0  B0  X
  1  A1  B1  Y
  2  A2  B2  X
  </code></pre>
  <p>And I want to merge the above with the following DataFrames,</p>
  <pre><code>df_x
  
      A   D
  0  A0  X0
  1  A1  X1
  2  A2  X2
  3  A3  X3
  </code></pre>
  <pre><code>df_y 
  
      A   D
  0  A0  Y0
  1  A1  Y1
  2  A2  Y2
  3  A3  Y3
  </code></pre>
  <p>When merging I want a select the second DataFrame based on the column <code>C</code>. In here, if the value in <code>C</code> is <code>X</code> then I need to use <code>df_x</code> to merge with that row, and similarly if the value in <code>C</code> is <code>Y</code> use <code>df_y</code>. So, the final output would be like,</p>
  <pre><code>    A   B  C   D
  0  A0  B0  X  X0
  1  A1  B1  Y  Y1
  2  A2  B2  X  X2
  </code></pre>
  <hr />
  <p>We may use some methods like, i) Iterating over each row and processing, or ii) Merging by adding <code>C</code> column for each <code>df_x</code> and <code>df_y</code> and then merging, etc. Obviously iterating method would not be much efficient. And the other method will consume additional space for a column with redundant data. Is there a <em>better</em> method to achieve this?</p>