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)

 

 

你可能感兴趣的:(python)