Python: Pandas Dataframe how to multiply entire column with a scalar
try using apply function.
df['quantity'] = df['quantity'].apply(lambda x: x*-1)
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 *=5df.two = df.two*5df.three = df.three.multiply(5)df['four'] = df['four']*5df.loc[:, 'five'] *=5df.iloc[:, 5] = df.iloc[:, 5]*5
which results in
one two three four five six0 5.0 5.0 5.0 5.0 5.0 5.01 5.0 5.0 5.0 5.0 5.0 5.02 5.0 5.0 5.0 5.0 5.0 5.03 5.0 5.0 5.0 5.0 5.0 5.04 5.0 5.0 5.0 5.0 5.0 5.0
Here's the answer after a bit of research:
df.loc[:,'quantity'] *= -1 #seems to prevent SettingWithCopyWarning