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

Ft 2020-10-12 AM 95℃ 0条

有兴趣研究可以点开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"]

解决方案有两种:

方案一:

在赋值时添加个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

非特殊说明,本博所有文章均为博主原创。

评论啦~