我有一个df
像这样的数据框:
measure model threshold
285 0.241715 a 0.0001
275 0.241480 a 0.0001
546 0.289773 b 0.0005
556 0.241715 b 0.0005
817 0.357532 a 0.001
827 0.269750 b 0.001
1088 0.489164 a 0.0025
我想将列中的所有值更改model
为'no_model'
。我该怎么做呢?
我目前正在做df['model'] = 'no_model'
,但是我得到了:
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
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
df['model'] = 'no_model'
之所以收到警告,是因为您可能引用了原始df:
df1 = df
然后尝试了您的代码,但您的意图是复制一个副本,因此您应该使用copy()
显式复制一个副本:
df_copy = df.copy()
这将摆脱警告
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句