Update pandas dataframe with values from another dataframe
<p>Assuming a dataframe where values from any of the columns can change, Given another dataframe which contains the old value, new value and column it belongs to, how to update dataframe using information about changes?
For example:</p>

<pre><code>&gt;&gt;&gt; my_df
    x    y    z
0   1    2    5
1   2    3    9
2   8    7    2
3   3    4    7
4   6    7    7
</code></pre>

<p><code>my_df_2</code> contains information about changed values and their columns:</p>

<pre><code>&gt;&gt;&gt; my_df_2
    changed_col    old_value    new_value
0      x              2             10
1      z              9             20
2      x              1             12
3      y              4             23
</code></pre>

<p>How to use information in <code>my_df_2</code> to update <code>my_df</code> such that <code>my_df</code> now becomes:</p>

<pre><code>&gt;&gt;&gt; my_df
    x     y     z
0   12    2     5
1   10    3     20
2   8     7     2
3   3     23    7
4   6     7     7
</code></pre>

<p>You can use the update method. See <a href="http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.DataFrame.update.html" rel="nofollow">http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.DataFrame.update.html</a></p>

<p>Example:</p>

<pre><code>old_df = pd.DataFrame({"a":np.arange(5), "b": np.arange(4,9)})

+----+-----+-----+
|    |   a |   b |
|----+-----+-----|
|  0 |   0 |   4 |
|  1 |   1 |   5 |
|  2 |   2 |   6 |
|  3 |   3 |   7 |
|  4 |   4 |   8 |
+----+-----+-----+

new_df = pd.DataFrame({"a":np.arange(7,8), "b": np.arange(10,11)})
+----+-----+-----+
|    |   a |   b |
|----+-----+-----|
|  0 |   7 |  10 |
+----+-----+-----+

old_df.update(new_df) 
+----+-----+-----+
|    |   a |   b |
|----+-----+-----|
|  0 |   7 |  10 | #Changed row
|  1 |   1 |   5 |
|  2 |   2 |   6 |
|  3 |   3 |   7 |
|  4 |   4 |   8 |
+----+-----+-----+
</code></pre>

python pandas