Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]])
like image 220
blaz Avatar asked May 26 '15 13:05

blaz


People also ask

How do I change row values in Pandas?

Suppose that you want to replace multiple values with multiple new values for an individual DataFrame column. In that case, you may use this template: df['column name'] = df['column name']. replace(['1st old value', '2nd old value', ...], ['1st new value', '2nd new value', ...])

How do you replace a specific value in a Pandas DataFrame?

Pandas DataFrame replace() MethodThe replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.

How do you replace a value in a DataFrame with another value?

replace() method. It is used to replace a regex, string, list, series, number, dictionary, etc. from a DataFrame, Values of the DataFrame method are get replaced with another value dynamically.


2 Answers

For the single row case:

In [35]:

df.loc[(df[0]==101) & (df[1]==3)] = [[200,10]]
df
Out[35]:
     0   1
0  100   0
1  100   1
2  101   2
3  200  10
4  102   4
5  102   5

For the multiple row-case the following would work:

In [60]:

a = np.array(([100, 100, 101, 101, 102, 102],
                 [0,1,3,3,3,4]))
df = pd.DataFrame(a.T)
df
Out[60]:
     0  1
0  100  0
1  100  1
2  101  3
3  101  3
4  102  3
5  102  4
In [61]:

df.loc[(df[0]==101) & (df[1]==3)] = 200,10
df
Out[61]:
     0   1
0  100   0
1  100   1
2  200  10
3  200  10
4  102   3
5  102   4

For multi-row update like you propose the following would work where the replacement site is a single row, first construct a dict of the old vals to search for and use the new values as the replacement value:

In [78]:

old_keys = [(x[0],x[1]) for x in old_vals]
new_valss = [(x[0],x[1]) for x in new_vals]
replace_vals = dict(zip(old_keys, new_vals))
replace_vals
Out[78]:
{(100, 0): array([300,  20]),
 (101, 3): array([200,  10]),
 (102, 5): array([400,  30])}

We can then iterate over the dict and then set the rows using the same method as my first answer:

In [93]:

for k,v in replace_vals.items():
    df.loc[(df[0] == k[0]) & (df[1] == k[1])] = [[v[0],v[1]]]
df
     0  1
0  100  0
     0  1
5  102  5
     0  1
3  101  3
Out[93]:
     0   1
0  300  20
1  100   1
2  101   2
3  200  10
4  102   4
5  400  30
like image 58
EdChum Avatar answered Sep 24 '22 08:09

EdChum


The simplest way should be this one:

df.loc[[3],0:1] = 200,10

In this case, 3 is the third row of the data frame while 0 and 1 are the columns.

This code instead, allows you to iterate over each row, check its content and replace it with what you want.

target = [101,3]
mod = [200,10]

for index, row in df.iterrows():
    if row[0] == target[0] and row[1] == target[1]:
        row[0] = mod[0]
        row[1] = mod[1]

print(df)
like image 28
alec_djinn Avatar answered Sep 23 '22 08:09

alec_djinn