How to multiply by a number the elements of a DataFrame column with pandas in python ?

Published: April 14, 2020

Tags: Python; Pandas; DataFrame;

DMCA.com Protection Status

Example of how to multiply by a number the elements of a DataFrame column with pandas in python

Create a simple Data frame

Let's create a data frame with pandas called df:

>>> import pandas as pd
>>> import numpy as np
>>> data = np.arange(1,13)
>>> data = data.reshape(3,4)
>>> df = pd.DataFrame(data=data,columns=['a','b','c','d'])
>>> df
   a   b   c   d
0  1   2   3   4
1  5   6   7   8
2  9  10  11  12

Multiply by a number the elements of a given column

Let's select the column b for example:

>>> df['b']
0     2
1     6
2    10

to multiply by 10 each element of the column b, a solution is to do:

>>> df['b'] = df['b'] * 2
>>> df
   a   b   c   d
0  1   4   3   4
1  5  12   7   8
2  9  20  11  12

References