0

I'm currently working on Kaggle's Titanic Survival Prediction. There's a problem when I wanted to encode the 'Sex' feature to 0 or 1. I've followed the official documentation on Pandas but it's does not help.

Edit: I've noticed that I wrote 'Male' instead of 'male' but still not working after i change to 'male'

I've followed the official documentation on Pandas but it's not working

Much Appreciated!

Danzeeeee
  • 2,588
  • 2
  • 16
  • 18
  • 2
    Possible duplicate of [Pandas: Replacing column values in dataframe](https://stackoverflow.com/q/23307301/1278112) – Shihe Zhang Dec 07 '17 at 06:24

1 Answers1

4

Try this:

df['Sex'] = df['Sex'].replace(['male', 'female'], [1,0])

or this

df['Sex'].replace(['male', 'female'], [1,0], inplace=True)

The problem is that you've set inplace=True while performing an assignment. This will change the series in place (i.e., it will change the Series object itself), which returns None for the entire series, which is wrong. So put inplace=False, or make an assignment, but not both. Using inplace=True changes the object and returns None, whereas inplace=False returns a new object.

offwhitelotus
  • 1,049
  • 9
  • 15