Categories
numpy pandas python

Replacing row values in pandas

I would like to replace row values in pandas.

In example:

import pandas as pd
import numpy as np
a = np.array(([100, 100, 101, 101, 102, 102],
np.arange(6)))
pd.DataFrame(a.T)

Result:

array([[100,   0],
[100, 1],
[101, 2],
[101, 3],
[102, 4],
[102, 5]])

Here, I would like to replace the rows with the values [101, 3] with [200, 10] and the result should therefore be:

array([[100,   0],
[100, 1],
[101, 2],
[200, 10],
[102, 4],
[102, 5]])

Update

In a more general case I would like to replace multiple rows.

Therefore the old and new row values are represented by nx2 sized matrices (n is number of row values to replace). In example:

old_vals = np.array(([[101, 3]],
[[100, 0]],
[[102, 5]]))
new_vals = np.array(([[200, 10]],
[[300, 20]],
[[400, 30]]))

And the result is:

array([[300,  20],
[100, 1],
[101, 2],
[200, 10],
[102, 4],
[400, 30]])