Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pandas: how to avoid chained assignment

Tags:

python

pandas

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

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

I understand I can avoid this by using .loc or .ix, but I would first need to find the location or the indices of all the rows which meet my condition of 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
like image 522
Pythonista anonymous Avatar asked Sep 02 '15 15:09

Pythonista anonymous


1 Answers

You should use loc to ensure you're working on a view, on your example the following will work and not raise a warning:

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

So the general form is:

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

The docs highlights the errors and there is the intro, granted some of the function docs are sparse, feel free to submit improvements.

like image 109
EdChum Avatar answered Sep 21 '22 13:09

EdChum