我有一个数据框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'
答案 0 :(得分:3)
您收到警告,因为您可能要么引用原始df:
df1 = df
然后尝试了您的代码,但您的目的是获取副本,因此您应该使用copy()
明确地获取副本:
df_copy = df.copy()
这将消除警告