ArtDijk
ArtDijk

Reputation: 2017

Pandas dataframe column calculation

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

Upvotes: 0

Views: 2168

Answers (1)

elyase
elyase

Reputation: 40963

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)

Upvotes: 1

Related Questions