How do I multiply each element of a given column of my dataframe with a scalar? (I have tried looking on SO, but cannot seem to find the right solution)
Doing something like:
df['quantity'] *= -1 # trying to multiply each row's quantity column with -1
gives me a 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
Note: If possible, I do not want to be iterating over the dataframe and do something like this...as I think any standard math operation on an entire column should be possible w/o having to write a loop:
for idx, row in df.iterrows():
df.loc[idx, 'quantity'] *= -1
EDIT:
I am running 0.16.2
of Pandas
full trace:
SettingWithCopyWarning:
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
dtype
. I can't replicate that error, it's also good to give full traceback. – Botherationdf = BigDF.query("X == 1")
ordf = BigDF[BigDF.X == 1]
or somesuch and that means df is actually just a view on BigDF. The warning is telling you that it is forcing it to make a copy, since otherwise it would cause a change in BigDF. – Melainemelameddf2 = df.copy()
. Then continue your code as before using the the copydf2['quantity'] *= -1
. If I am doing something wrong please don't slaughter me, I am a beginner however this solution removed the warning for me. Please correct me if I am giving the incorrect solution. – Winser