SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 解决方法

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 解决方法,第1张

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 解决方法

1、首先展示下遇到的问题截图

2、然后跟着这个错误的报错信息,点开下面链接

有兴趣研究可以点开https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

蹩脚英语看半天没看明白,转战为自己研究

这个报警主要是在说,当你在采用这种链式赋值时,当你修改df_1时,df也可能随之变化

3、简单代码复现问题

import pandas as pd
import numpy as np
 
df = pd.Dataframe(np.random.randint(1,10,(4,5)),columns=["A","B","C","D","E"])
df_1 = df[['A', 'B']]
df_1["X"]= df_1["A"] +df_1["B"]

然后尝试了很久,跟解释里说的有没有用什么链式引用(需要用loc)没太大关系。(8月6日更新)
 

5、解决方案:

解决方案有两种:

方案一:

赋值时添加个copy(),确保两个值不相同:

df = pd.Dataframe(np.random.randint(1,10,(4,5)),columns=["A","B","C","D","E"])
print(df)
df_1 = df[["A","B"]].copy()
df_1["A"]= df_1["A"] +1
print("df = ",df)
print("df_1 = ",df_1)

方案二:

当需要把dataframe的部分赋值给另一个dataframe时,也可以采用loc

df = pd.Dataframe(np.random.randint(1,10,(4,5)),columns=["A","B","C","D","E"])
print(df)
df_1 = df.loc[:,["A","B"]]
df_1["A"]= df_1["A"] +1
print("df = ",df)
print("df_1 = ",df_1)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5657746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存