python astype(str) gives SettingWithCopyWarning and requests I use loc

Viewed 4376

Using this simple line of code, I keep on getting a SettingWithCopyWarning error that than carries through my whole code.

#make email a string
df['Email Address'] = df['Email Address'].astype(str)

C:\Users\xxx\AppData\Local\Continuum\Anaconda2\lib\site-packages\ipykernel\__main__.py:2: 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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  from ipykernel import kernelapp as app

I went through the documentation, but can't make it work with loc. The code below is wrong.

df.loc['Email Address'] = df.loc['Email Address'].astype(str)

Please excuse if this is a duplicate question - I searched it on stackoverflow, but couldn't find one that addresses loc and astype.

2 Answers

Update 03/21

I believe this is the correct solution with loc

df.loc[:, 'Email Address'].astype(str)

Related