Dhruv Ghulati Dhruv Ghulati - 1 year ago 181
Python Question

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

I have a data frame


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
. How do I do this?

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

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download