Python: Pandas Dataframe how to multiply entire column with a scalar

pandas multiply multiple columns by scalar
pandas multiply column by a scalar
pandas multiply column by float
pandas divide each column by it's sum
multiply dataframe column by scalar r
pandas multiply a column by another column
pandas groupby multiply two columns
multiply each column of a dataframe by a vector

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

try using apply function.

df['quantity'] = df['quantity'].apply(lambda x: x*-1)

Python: Pandas Dataframe how to multiply entire column with a scalar, If your dataframe is read with no headers then your index will be an integer, not a string. For example take this data saved as fake.csv. Output:. Python: Pandas Dataframe how to multiply the entire column by a scalar 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 -

Note: for those using pandas 0.20.3 and above, and are looking for an answer, all these options will work:

df = pd.DataFrame(np.ones((5,6)),columns=['one','two','three',
                                       'four','five','six'])
df.one *=5
df.two = df.two*5
df.three = df.three.multiply(5)
df['four'] = df['four']*5
df.loc[:, 'five'] *=5
df.iloc[:, 5] = df.iloc[:, 5]*5

which results in

   one  two  three  four  five  six
0  5.0  5.0    5.0   5.0   5.0  5.0
1  5.0  5.0    5.0   5.0   5.0  5.0
2  5.0  5.0    5.0   5.0   5.0  5.0
3  5.0  5.0    5.0   5.0   5.0  5.0
4  5.0  5.0    5.0   5.0   5.0  5.0

pandas.DataFrame.multiply, How do you split a Dataframe in Python based on column values? DataFrame.multiply(self, other, axis='columns', level=None, fill_value=None) [source] ¶ Get Multiplication of dataframe and other, element-wise (binary operator mul). Equivalent to dataframe * other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rmul.

Here's the answer after a bit of research:

df.loc[:,'quantity'] *= -1 #seems to prevent SettingWithCopyWarning 

Multiply entire column with a constant with pandas in python, How do you add a column to a Dataframe in Python? You can can do that either by just multiplying or dividing the columns by a number (mul = *, Div = /) or you can perform scalar operation (mul, div, sum, sub,…) direct on any numeric column as show below or you could use the apply method on a columns of the dataframe:

More recent pandas versions have the pd.DataFrame.multiply function.

df['quantity'] = df['quantity'].multiply(-1)

Multiplying a Pandas Series with another Series, or you can perform scalar operation (mul, div, sum, sub,…) direct on any How do I add a new column to an existing data frame in Pandas? 3,878 Views. Multiply entire column with a constant with pandas in python Multiply entire column with a constant with pandas in python. add formatted column to pandas data

A bit old, but I was still getting the same SettingWithCopyWarning. Here was my solution:

df.loc[:, 'quantity'] = df['quantity'] * -1

How to split a pandas DataFrame into multiple DataFrames by , We make Stack Overflow and 170+ other community-powered Q&A sites. [*] I have checked that this issue has not already been reported. [*] I have confirmed this bug exists on the latest version of pandas. (optional) I have confirmed this bug exists on the master branch of pandas.

Python Pandas DataFrame: load, edit, view data, Example #1: Use Series.multiply() function to perform the multiplication of a scalar with the given series object. I'm trying to multiply a column by a float. Browse other questions tagged python pandas floating-point multiplication or ask Delete column from pandas

How to multiply or divide a column by a fixed number in a Pandas , Pandas dataframe.mul() function return multiplication of dataframe and other Syntax: DataFrame.mul(other, axis='columns', level=None, fill_value=None). This method returns an iterable tuple (index, value). I have a pandas data frame (X11) like this: In actual I have 99 columns up to dx99. Iterate over (column name, Series) pairs. sum() C:\pandas > python example40. Iterating over rows and columns in Pandas DataFrame Iteration is a general term for taking each item of something, one after another.

Python, Using multiply method and then set axis="index": df[["A", "B"]].multiply(df["C"], axis​="index"). If you want to learn more about Data Science, visit  I am trying to multiply each row of a pandas dataframe by a different value and wondering what the best way to do this is. For example if I have the following dataframe: import numpy as np import pandas as pd df = pd.DataFrame(np.random.randn(2, 3)) df 0 1 2 0 -1.283316 0.849488 1.936060 1 -2.078575 -0.871570 -0.970261

Comments
  • Check type of that column using dtype. I can't replicate that error, it's also good to give full traceback.
  • I've edited to add full trace...also its not an error, its a warning (for clarity)
  • I think it's being caused by something other than that line, or maybe that line is causing the warning to rise that was generated from earlier. What you're getting is related to slicing the dataframe.
  • Curious, did you ever figure this out? I'm dealing with the same problem.
  • this is pretty graceful when compared to looping, though I still get the SettingWithCopyWarning
  • Series.apply is a loop and should not be used for simple multiplication. The unnecessary lambda only makes it worse.
  • i tried this and my allocation which is running 1.2 sec now running in 0.05 sec
  • @ALollz, indexing using Attribute Access, is still in the user guide as of the 1.0, I think you are referring to the warning from creating a new column. The usage of df.col_name = is valid here
  • This throws a SettingWithCopyWarning in pandas 0.18.0.
  • Seems outrageous how many gotchas there are in Pandas, and how much easier this is in R: require(data.table); df[,quantity]*-1. No need to remember colons, .ix,.loc, iloc, quoting field names, nor updating copies when you meant to update the original.
  • The real problem of why you are getting the error is not that there is anything with your code: you can use iloc, loc, or apply. The real problem that you have is due to how you created the df DataFrame. Most likely you created your df as a slice of another DataFrame without using .copy(). The correct way to create your df as a slice of another DataFrame is df = original_df.loc[some slicing].copy().
  • This should now be the accepted answer. Adding .copy() to the previous slicing operation is the key to prevent the mentioned warning.
  • this is no different than df['quantity'] *= -1 (and yes I get the same warning)
  • this is the only mentioned solution that is working and doesn't throw the warning