2

I'm trying to update the pandas data frame by logical condition but, it fails with below error,

df[df.b <= 0]['b'] = 0 

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

How do I get this working?

Data:

df = pd.DataFrame({'a': np.random.randn(4), 'b': np.random.randn(4)}) a b 0 1.462028 -1.337630 1 0.206193 -1.060710 2 -0.464847 -1.881426 3 0.290627 0.650805 

I am learning pandas. In R, syntax is like below,

df[df$b <= 0]$b <- 0 

3 Answers 3

7

Use

df.loc[df.b <= 0, 'b']= 0 

For efficiency pandas just creates a references from the previous DataFrame instead of creating new DataFrame every time a filter is applied.
Thus when you assign a value to DataFrame it needs tobe updated in the source DataFrame (not just the current slice of it). This is what is refered in the warning

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

To avoid this .loc syntax is used.

For more information on DataFrame indexing

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

>>> df.ix[df['b']<=0] = 0 >>> df a b 0 0.000000 0.000000 1 0.000000 0.000000 2 0.212535 0.491969 3 0.000000 0.000000 

Note: Since v0.20 ix has been deprecated. Use loc or iloc instead.

Comments

0

Follow below pattern for updating the value -

food_reviews_df.loc[food_reviews_df.Score <= 3, 'Score'] = 0 food_reviews_df.loc[food_reviews_df.Score >= 4, 'Score'] = 1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.