Python pandas: how to avoid bound assignment

I have a pandas dataframe with two columns: x and value. I want to find all lines where x == 10, and for all these lines set value = 1000. I tried the code below, but I get a warning that

A value is trying to be set on a copy of a slice from a DataFrame. 

I understand that I can avoid this using .loc or .ix, but first I need to find the location or indexes of all the rows that match my state x == 10. Is there a more direct way?

Thanks!

 import numpy as np import pandas as pd df=pd.DataFrame() df['x']=np.arange(10,14) df['value']=np.arange(200,204) print df df[ df['x']== 10 ]['value'] = 1000 # this doesn't work print df 
+5
source share
1 answer

You must use loc to make sure that you are working on a view, in your example, the following will work, and not raise a warning:

 df.loc[df['x'] == 10, 'value'] = 1000 

So, the general view:

 df.loc[<mask or index label values>, <optional column>] = < new scalar value or array like> 

docs highlights errors and there is intro , some of the docs functions provided are sparse, feel free to report improvements.

+3
source

All Articles