python:A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_inde...

1.运行一段代码,出现警示错误,但是程序还是正常运行

file_hw = file[file["厂家"] == "华为"]
file_hw["ECI"] = file_hw.loc[:, "ENODEB"].astype(str) + "-" + file_hw.loc[:, "小区标示"].astype(str)
  • 错误如下:
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
# 。。。。。。。。省略部分错误
  • 解决方案:
file_hw = file[file["厂家"] == "华为"]
x = file_hw.copy
x["ECI"] = x.loc[:, "ENODEB"].astype(str) + "-" + x.loc[:, "小区标示"].astype(str)
# This warning comes because your dataframe x is a copy of a slice. This is not easy to know why, but it has something to do with how you have come to the current state of it.
# You can either create a proper dataframe out of x by doing
x = x.copy()
# This will remove the warning, but it is not the proper way
# You should be using the DataFrame.loc method, as the warning suggests, like this:
x.loc[:,'Mass32s'] = pandas.rolling_mean(x.Mass32, 5).shift(-2)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。