title: pandas DataFrame concat / update ("upsert")?
body: <p>I am looking for an elegant way to append all the rows from one DataFrame to another DataFrame (both DataFrames having the same index and column structure), but in cases where the same index value appears in both DataFrames, use the row from the second data frame.</p>
  
  <p>So, for example, if I start with:</p>
  
  <pre><code>df1:
                      A      B
      date
      '2015-10-01'  'A1'   'B1'
      '2015-10-02'  'A2'   'B2'
      '2015-10-03'  'A3'   'B3'
  
  df2:
      date            A      B
      '2015-10-02'  'a1'   'b1'
      '2015-10-03'  'a2'   'b2'
      '2015-10-04'  'a3'   'b3'
  </code></pre>
  
  <p>I would like the result to be:</p>
  
  <pre><code>                    A      B
      date
      '2015-10-01'  'A1'   'B1'
      '2015-10-02'  'a1'   'b1'
      '2015-10-03'  'a2'   'b2'
      '2015-10-04'  'a3'   'b3'
  </code></pre>
  
  <p>This is analogous to what I think is called "upsert" in some SQL systems --- a combination of update and insert, in the sense that each row from <code>df2</code> is either (a) used to update an existing row in <code>df1</code> if the row key already exists in <code>df1</code>, or (b) inserted into <code>df1</code> at the end if the row key does not already exist.</p>
  
  <p>I have come up with the following </p>
  
  <pre><code>pd.concat([df1, df2])     # concat the two DataFrames
      .reset_index()        # turn 'date' into a regular column
      .groupby('date')      # group rows by values in the 'date' column
      .tail(1)              # take the last row in each group
      .set_index('date')    # restore 'date' as the index
  </code></pre>
  
  <p>which seems to work, but this relies on the order of the rows in each groupby group always being the same as the original DataFrames, which I haven't checked on, and seems displeasingly convoluted.</p>
  
  <p>Does anyone have any ideas for a more straightforward solution?</p>