0

I can't seem to understand why I am getting a warning from my pandas code below: I am trying to convert the column 'Sex' in the titanic.csv dataset to numeric using the code below:

from sklearn import preprocessing
label_encoding = preprocessing.LabelEncoder()
titanic_df['Sex'] =label_encoding.fit_transform(titanic_df['Sex'].astype(str))
titanic_df.head()

I get this warning:

<ipython-input-41-45071618b7c1>:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  titanic_df['Sex'] =label_encoding.fit_transform(titanic_df['Sex'].astype(str))

How do I format the code to avoid getting errors?

Alex
  • 21
  • 1
  • 3

0 Answers0