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

在使用DataFrame赋值时,遇到报错警告如下:

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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  df['safe_fac_s1'][0] = 1

经查发现错误是由于df的赋值引起的

df['safe_fac_s1'][0] = 1
df['safe_fac_s2'][0] = 1

正确的赋值方式:

df.loc[0,'safe_fac_s1'] = 1
df.loc[0,'safe_fac_s2'] = 1

注意要使用df.loc[‘行标签’,‘列标签’] 来赋值。

具体原因可参考:https://blog.csdn.net/qq_42711381/article/details/90451301

你可能感兴趣的:(python学习,python,开发语言,后端)