Kakarot_7
Kakarot_7

Reputation: 342

Replace specific entries in a column of dataframe

I want to replace all the 'male' in the 'Sex' column to 1 and all the 'female' to 0.

Here is my dataframe df:

    Pclass    Sex        Age    SibSp   Parch
0   3        male       22.0    1         0
1   1        female     38.0    1         0
2   3        female     26.0    0         0
3   1        female     35.0    1         0
4   3        male       35.0    0         0
5   3        male       2.0     3         1
6   3        female     27.0    0         2
7   2        female     14.0    1         0
8   3        female     4.0     1         1
9   1        female     58.0    0         0

I used the pd.get_dummies:

pd.get_dummies(df.Sex)

But it creates two columns male and female like this:

   female   male
0    0       1
1    1       0
2    1       0
3    1       0
4    0       1

On the Other hand I want only a single column of sex denoting 1 for male and 0 for female.

I know that i can use a for loop to iterate over all the rows and change but is there a pandorable way to do this??

Upvotes: 1

Views: 156

Answers (4)

sigmapie8
sigmapie8

Reputation: 467

Please make sure there are only 'male' & 'female' values for 'Sex' column by df['Sex'].value_counts() If that's the case then

df['Sex'] = df['Sex'].apply(lambda x:1 if x == 'male' else 0)

Upvotes: 0

Dishin H Goyani
Dishin H Goyani

Reputation: 7693

Compare with == and then convert boolean type result to int type.

df['Sex_'] = (df.Sex == 'male').astype(int)

or If you want to use your current code pd.get_dummies you can just delete one column

df['Sex_'] = pd.get_dummies(df.Sex, drop_first = True)

Upvotes: 1

Akhilesh_IN
Akhilesh_IN

Reputation: 1317

If you need to map many labels, try:

dicts = {"male":1,"female":0}
df['Sex'].map(dicts)

Upvotes: 0

oreopot
oreopot

Reputation: 3450

Try the following code:

df["Sex"].replace({"male": 1, "female": 0}, inplace=True)

Upvotes: 3

Related Questions