Dhruv Ghulati Dhruv Ghulati - 2 months ago 6
Python Question

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

I have a data frame

df
like:

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


I want to change all values in the column
model
to
'no_model'
. How do I do this?

I am currently doing
df['model'] = 'no_model'
, but I'm getting:

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'

Answer

You get the warning because you probably either made a reference to the original df:

df1 = df

and then tried your code but your intention was to take a copy so you should use copy() to explicitly take a copy:

df_copy = df.copy()

this will get rid of the warning

Comments