已解决SettingWithCopyWarning Try using .loc[row_indexer,col_indexer] = value instead

已解决(pandas赋值错误)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









报错代码



粉丝群一个小伙伴在使用pandas对DataFrame中的某一列进行修改赋值时给出如下警告(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下:

import pandas as pd
import sqlalchemy

df = pd.read_excel("./test.xls")

temp = df[['name', 'age']]
temp['address'] = '四川'
print(temp)

# df.to_sql("ORG_HONOR", con=engine, if_exists="append", index=False, chunksize=2000)


报错信息截图


在这里插入图片描述





报错翻译



报错信息翻译

设置有复制警告:
试图在数据帧的切片副本上设置值。
尝试改用.loc[row_indexer,col_index]=值
请参阅文档中的注意事项:https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a视图与a副本





报错原因



首先要理解的是,SettingWithCopyWarning 是一个警告,而不是错误 Error。

错误表明某些内容是“坏掉”的,例如无效语法(invalid syntax)或尝试引用未定义的变量。警告的作用是提醒程序员,他们的代码可能存在潜在的错误或问题,但是这些操作仍然是该编程语言中的合法操作。在这种情况下,警告很可能表明一个严重但不容易意识到的错误。SettingWithCopyWarning 告诉你,你的操作可能没有按预期运行,你应该检查结果以确保没有出错。


这是在警告你类似这种的赋值,请使用.loc方法,理解这个之前还是想介绍一下pandas的视图模式和副本模式:

视图模式
    将一个对象整体赋值给另一个变量
    修改一个变量,另一个变量值也会变
    多个变量数据指向同一内存数据

副本模式
    将一个对象查询的一部分值赋值给另一个变量
    修改一个变量,另一个变量值不会变

当将一个对象整体赋值给另一个变量时,视图模式,两个变量对应的内存地址相同,修改一个变量,另一个变量也会改变。

当使用copy()将一个对象赋值给另一个变量时
    或者使用查询赋值,查询数据的一部分并赋值给其他变量
    当赋值为原数据查询的一部分时,是副本模式,修改一个变量不会影响另一个变量



解决方法



使用 .loc[row_indexer,col_indexer] = value instead进行赋值:

temp.loc[:,'address'] = '四川'

以上是此问题报错原因的解决方法,欢迎评论区留言讨论是否能解决,如果有用欢迎点赞收藏文章谢谢支持,博主才有动力持续记录遇到的问题!!!

千人全栈VIP答疑群联系博主帮忙解决报错

由于博主时间精力有限,每天私信人数太多,没办法每个粉丝都及时回复,所以优先回复VIP粉丝,可以通过订阅限时9.9付费专栏《100天精通Python从入门到就业》进入千人全栈VIP答疑群,获得优先解答机会(代码指导、远程服务),白嫖80G学习资料大礼包,专栏订阅地址:https://blog.csdn.net/yuan2019035055/category_11466020.html

  • 优点作者优先解答机会(代码指导、远程服务),群里大佬众多可以抱团取暖(大厂内推机会),此专栏文章是专门针对零基础和需要进阶提升的同学所准备的一套完整教学,从0到100的不断进阶深入,后续还有实战项目,轻松应对面试!

  • 专栏福利简历指导、招聘内推、每周送实体书、80G全栈学习视频、300本IT电子书:Python、Java、前端、大数据、数据库、算法、爬虫、数据分析、机器学习、面试题库等等

  • 注意:如果希望得到及时回复,订阅专栏后私信博主进千人VIP答疑群在这里插入图片描述
    在这里插入图片描述

免费资料获取,更多粉丝福利,关注下方公众号获取

在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
TCP/IP Sockets In C 第二版 英文版 Changes from the First Edition We have updated and considerably expanded most of the material, having added two chapters. Major changes from the first edition include:  IP version 6 coverage. We now include three kinds of code: IPv4-specific, IPv6-specific, and generic. The code in the later chapters is designed to work with either protocol version on dual-stack machines.  An additional chapter on socket programming in C++ (contributed by David B. Sturgill). The PracticalSocket library provides wrappers for basic socket functionality. These allow an instructor to teach socket programming to students without C programming back- ground by giving them a library and then gradually peeling back the layers. Students can start developing immediately after understanding addresses/ports and client/server. Later they can be shown the details of socket programming by peeking inside the wrapper code. Those teaching a subject that uses networking (e.g., OS) can use the library and only selectively peel back the cover.  Enhanced coverage of data representation issues and strategies for organizing code that sends and receives messages. In our instructional experience, we find that students have less and less understanding of how data is actually stored in memory, 1 so we have attempted to compensate with more discussion of this important issue. At the same time, internationalization will only increase in importance, and thus we have included basic coverage of wide characters and encodings.  Omission of the reference section. The descriptions of most of the functions that make up the Sockets API have been collected into the early chapters. However, with so many online sources of reference information—including “man pages”—available, we chose to leave out the complete listing of the API in favor of more code illustrations.  Highlighting important but subtle facts and caveats. Typographical devices call out important concepts and information that might otherwise be missed on first reading. Although the scope of the book has expanded, we have not included everything that we might have (or even that we were asked to include); examples of topics left for more comprehensive texts (or the next edition) are raw sockets and programming with WinSock.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁袁袁袁满

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值