uharsha33
uharsha33

Reputation: 225

How to replace an Object in pandas dataframe with another value in python

I have a dataframe that looks similar to this.

age gender edu income
15    m     MS   <=50
16    f     BS   >50
17    m     BS   <=50

Since this is a binary problem, i'd like all the <=50K values to be 0 and >50K to be 1. I've tried replace method and it didn't do anything.

data["income"].replace(["<=50K"], "0", inplace = True)

data["income"].replace( to_replace = "<=50K"], value = 0, inplace = True)

Upvotes: 1

Views: 2722

Answers (2)

BENY
BENY

Reputation: 323356

Using map

df.income=df.income.map({'<=50':0,'>50':1})
df
Out[328]: 
   age gender edu  income
0   15      m  MS       0
1   16      f  BS       1
2   17      m  BS       0

Upvotes: 2

Scott Boston
Scott Boston

Reputation: 153510

IIUC:

data['income'] = (data.income == '>50').astype(int)

Output:

   age gender edu  income
0   15      m  MS       0
1   16      f  BS       1
2   17      m  BS       0

Upvotes: 4

Related Questions