Example of how to divide by a number the elements of a pandas data frame column 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'])>>> dfa b c d0 1 2 3 41 5 6 7 82 9 10 11 12
Divide by a number the elements of a given column
Let's select the column b for example:
>>> df['b']0 21 62 10
to divide by 2 each element of the column b, a solution is to do:
>>> df['b'] = df['b'] / 2.0>>> dfa b c d0 1 1.0 3 41 5 3.0 7 82 9 5.0 11 12
References
| Links | Site |
|---|---|
| Apply a function to a single column in Dataframe | thispointer.com |
| pandas.DataFrame.apply | pandas doc |
