【问题标题】:A value is trying to be set on a copy of a slice from a DataFrame Error试图在来自 DataFrame 错误的切片副本上设置值
【发布时间】:2021-08-09 20:01:30
【问题描述】:

我有这个代码 sn-p 但我收到了警告。怎么能避免这种情况。 我搜索了这个问题,但找不到任何可以解决我的问题。

这是收到警告的代码,(df_test_arima 是 pandas 数据帧)

df_test_arima['ACTUAL'] = y_test

y_test 来自 ;

X_train,X_test,y_train,y_test = train_test_split(X.index,y, shuffle = False, test_size=0.050)

警告我得到了

SettingWithCopyWarning:试图在一个副本上设置一个值 从 DataFrame 切片。尝试使用 .loc[row_indexer,col_indexer] = 取而代之的价值

谢谢你:)

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

尝试loc(复制所有对应的行,列)

df_test_arima['ACTUAL'] = y_test.loc[:,:]

另一种方法是禁用文件中的所有此类警告(但不推荐)

import warnings
warnings.filterwarnings("ignore", category=SettingWithCopyWarning)

【讨论】:

  • 谢谢Pawan,第一种方法不起作用,我仍然得到同样的错误。
猜你喜欢
  • 2016-12-22
  • 2018-03-25
  • 1970-01-01
  • 2017-06-19
  • 2016-09-23
  • 2016-02-17
  • 2016-08-24
  • 2016-06-16
相关资源
最近更新 更多