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 Answers1

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.

jtan354
  • 84
  • 9
  • Weird, I'm not getting the error for that line. I only get it on for x in passengers_copy['Embarked'] . – Maria Contreras Jul 17 '19 at 06:36
  • @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. – jtan354 Jul 17 '19 at 06:40
  • @MariaContreras if my answer has helped you please accept it as an official answer, thank you! – jtan354 Jul 17 '19 at 06:41