python astype(str)给出了SettingWithCopyWarning和请求我使用loc

使用这个简单的代码行,我继续得到一个SettingWithcopyWarning错误,而不是通过我的整个代码.

#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

我浏览了文档,但无法使用loc.下面的代码错误的.

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

请原谅这是一个重复的问题 – 我在stackoverflow上搜索它,但找不到一个解决loc和astype的问题.

解决方法

您的问题不在于您如何进行作业.它与分配前的数据帧有关.在赋值之前的某个时刻,您创建了df,使其成为另一个数据帧的视图.您可以使用bool验证这一点(df.is_copy)

如果你没有把df作为一个单独的东西,没有与其他数据帧中的数据的链接……

df = df.copy()

然后继续进行作业.

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...