Python pandas equivalent for replace

ivan-k picture ivan-k · Aug 28, 2012 · Viewed 32.9k times · Source

In R, there is a rather useful replace function. Essentially, it does conditional re-assignment in a given column of a data frame. It can be used as so: replace(df$column, df$column==1,'Type 1');

What is a good way to achieve the same in pandas?

Should I use a lambda with apply? (If so, how do I get a reference to the given column, as opposed to a whole row).

Should I use np.where on data_frame.values? It seems like I am missing a very obvious thing here.

Any suggestions are appreciated.

Answer

DSM picture DSM · Aug 28, 2012

pandas has a replace method too:

In [25]: df = DataFrame({1: [2,3,4], 2: [3,4,5]})

In [26]: df
Out[26]: 
   1  2
0  2  3
1  3  4
2  4  5

In [27]: df[2]
Out[27]: 
0    3
1    4
2    5
Name: 2

In [28]: df[2].replace(4, 17)
Out[28]: 
0     3
1    17
2     5
Name: 2

In [29]: df[2].replace(4, 17, inplace=True)
Out[29]: 
0     3
1    17
2     5
Name: 2

In [30]: df
Out[30]: 
   1   2
0  2   3
1  3  17
2  4   5

or you could use numpy-style advanced indexing:

In [47]: df[1]
Out[47]: 
0    2
1    3
2    4
Name: 1

In [48]: df[1] == 4
Out[48]: 
0    False
1    False
2     True
Name: 1

In [49]: df[1][df[1] == 4]
Out[49]: 
2    4
Name: 1

In [50]: df[1][df[1] == 4] = 19

In [51]: df
Out[51]: 
    1   2
0   2   3
1   3  17
2  19   5