0

I'm trying to map some non numerical values in a pandas dataset that contains information from passengers that were on the Titanic. Right now I'm trying to map the ports where passengers embarked from. There are 3 ports and I'm trying to map them by adding a column for each port. 0 if they embarked from that port 1 if not.

I tried doing something similar with sex. I added another column with a 0 if the sex was male and 1 if the sex was female. That worked, but when I try doing this with the ports I get a "'float' object is not subscriptable" error.

import pandas as pd
#opening the data
train_path = #path to data set
passengers = pd.read_csv(train_path)

#creating a copy
passengers_copy = passengers

#mapping sex
passengers_copy['SexBin'] = [0 if x[0] == 'm' else 1 for x in passengers_copy['Sex']]

#trying to map the first port
passengers_copy['EmbarkedS'] = [0 if x[0] == 'S' else 1 for x in passengers_copy['Embarked']]
1
  • I'm trying to run this but passengers_copy is not defined. Please provide everything so we can test your code. Commented Jul 17, 2019 at 6:26

1 Answer 1

2

In this: For x in passengers_copy['Embarked'] x is already a float.

With a float, it is not subscriptable (meaning that unlike a list, it cannot be subscripted with square brackets to access the specific index.) because it is simply a number and not a string/list.

When you try x[0], that is what is causing the error.

Sign up to request clarification or add additional context in comments.

3 Comments

Weird, I'm not getting the error for that line. I only get it on for x in passengers_copy['Embarked'] .
@MariaContreras my mistake, i meant to write Embarked. This is because in your Embarked column, it is a number 0 or 1 right? As for your Sex column, it is a string 'm' or 'f' i presume.
@MariaContreras if my answer has helped you please accept it as an official answer, thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.