0

I want to replace the values in a column by a calulation (which is a multiplcation of comlun values). like this:

    df.loc[:, 'cfit'] = df['cfit'] * df['risk_nr']

I get the following message: 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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self.obj[item] = s

Is this a correct or false warning of pandas ? Thnx

2
  • What version of pandas are you running and are you able to post sample data to reproduce your problem as I cannot Commented Nov 30, 2014 at 16:18
  • pandas version 0.15.1 Commented Nov 30, 2014 at 19:58

1 Answer 1

1

Just do it like this:

df['cfit'] = df['cfit'] * df['risk_nr']

or even:

df['cfit'] *= df['risk_nr']

Regarding your question: I think this is a false warning in this particular case, as df.loc[:, 'cfit'] should return a view and not a copy. You can turn off the warning with:

pd.set_option('chained_assignment', None)
Sign up to request clarification or add additional context in comments.

2 Comments

I'm inclined to agree with you that it's a false warning. I've seen that warning before, but it's generally associated with a conditional update not using .loc/row index.
@BobHaffner, Yep, I think something else is going on there as I don't get the warning for a simple df.loc[:, 'colname'] = something. May be the code shown is not the actual one he is using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.