Pythonista anonymous
Pythonista anonymous

Reputation: 8950

python pandas: how to avoid chained assignment

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

Upvotes: 10

Views: 4559

Answers (1)

EdChum
EdChum

Reputation: 394091

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.

Upvotes: 7

Related Questions