1

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??

Kakarot_7
  • 302
  • 1
  • 5
  • 14
  • Does this answer your question? [Remap values in pandas column with a dict](https://stackoverflow.com/questions/20250771/remap-values-in-pandas-column-with-a-dict) – KenHBS May 22 '20 at 08:03

4 Answers4

3

Try the following code:

df["Sex"].replace({"male": 1, "female": 0}, inplace=True)
oreopot
  • 3,392
  • 2
  • 19
  • 28
1

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)
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
0

If you need to map many labels, try:

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

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)
sigmapie8
  • 451
  • 3
  • 9