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)
  • stackoverflow解决方案连接:
    https://stackoverflow.com/questions/31468176/setting-values-on-a-copy-of-a-slice-from-a-dataframe?rq=1
# 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)

你可能感兴趣的:(python:A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_inde...)