加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:01:45 所属栏目:Python 来源:网络整理
导读:使用这个简单的代码行,我继续得到一个SettingWithCopyWarning错误,而不是通过我的整个代码. #make email a stringdf['Email Address'] = df['Email Address'].astype(str)C:UsersxxxAppDataLocalContinuumAnaconda2libsite-packagesipykernel__mai
使用这个简单的代码行,我继续得到一个SettingWithCopyWarning错误,而不是通过我的整个代码.

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

C:UsersxxxAppDataLocalContinuumAnaconda2libsite-packagesipykernel__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()

然后继续进行作业.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读