+2 votes
in Programming Languages by (74.2k points)
In one of the columns of a Pandas DataFrame, the value are 'pos' and 'neg'. I want to replace 'pos' with 1 and 'neg' with 0. How can I do it?

1 Answer

+1 vote
by (351k points)
selected by
 
Best answer

You can use the replace() function of Pandas. You need to pass a dictionary to this function in this format:

{column_name:{'old value1':'new value1', 'old value2':'new value2',...}}

Here is an example to illustrate it:

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({'id1':[1,2,3,4,5,6,7,8,9,10],'class':['pos','pos','neg','pos','neg','neg','pos','neg','neg','pos']})
>>> df
   id1 class
0    1   pos
1    2   pos
2    3   neg
3    4   pos
4    5   neg
5    6   neg
6    7   pos
7    8   neg
8    9   neg
9   10   pos
>>> df1=df.replace({'class':{'pos':1, 'neg':0}})
>>> df1
   id1  class
0    1      1
1    2      1
2    3      0
3    4      1
4    5      0
5    6      0
6    7      1
7    8      0
8    9      0
9   10      1


...