更改 pandas 数据框中一列中所有行的值

Change value of all rows in a column of pandas data frame

我有一个数据框 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()

这将消除警告