【问题标题】:SettingWithCopyWarning - How do I fix this warning?SettingWithCopyWarning - 如何修复此警告?
【发布时间】:2021-07-19 20:28:38
【问题描述】:

我在以下代码中收到SettingWithCopyWarning

combined_updated['institute_service'] = combined_updated['institute_service'].copy().astype('float')

社区中的人可以帮助我了解为什么我不断收到该警告吗?

【问题讨论】:

  • 谢谢。我最初看到这篇文章,它通过索引以下代码帮助我修复了警告:combined_updated.loc[combined_updated['institute_service']=='Less than 1 year','institute_service']=combined_updated['institute_service'][combined_updated['institute_service']=='Less than 1 year'].str.replace('Less than 1 year', '1.0')。现在的问题是我在最初问题中的代码中不断收到相同的警告。

标签: python string dataframe warnings


【解决方案1】:

不需要使用 copy() 将列更改为浮动;也许 .copy() 给你警告。您也可以在执行操作之前尝试重置索引,当我收到该警告时,这通常对我有用。

combined_updated = combined_updated.reset_index(drop=True)
combined_updated['institute_service'] = combined_updated['institute_service'].astype(float)

【讨论】:

  • 谢谢@Hammurabi。仍然收到相同的警告,但消息不同:self.obj[item] = s。任何额外的帮助将不胜感激。
  • 你能用 self.obj[item] 更新你的帖子吗?我不清楚你是如何从 combine_updated 到 self.obj[item] 的。
  • 你好@Hammurabi,这是给我警告信息的单元格:combined_updated.loc[combined_updated['institute_service']=='7-10','institute_service']=combined_updated['institute_service'][combined_updated['institute_service']=='7-10'].str.replace('7-10', '10.0') combined_updated = combined_updated.reset_index(drop = True) combined_updated['institute_service'] = combined_updated['institute_service'].astype(float)
  • 抱歉,我不知道您收到警告的原因,我无法复制该错误。另外,我不明白您如何从中获得 self.obj[item] == s。也许只尝试 .head(5) 看看你是否在前 5 行得到错误,然后是前 10 行,等等?也许你的df中有一些意想不到的东西。
猜你喜欢
  • 2023-01-04
  • 2019-12-07
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 2022-10-31
  • 1970-01-01
  • 2019-08-08
  • 2020-11-28
相关资源
最近更新 更多