Subtract every column in dataframe with the mean of that column with Python

haimen picture haimen · Feb 3, 2016 · Viewed 13.4k times · Source

I am looking for a way to find means of each column in a python dataframe and subtract that column with the mean of that column.

Suppose I have:

df = pd.DataFrame({'a': [1.5, 2.5], 'b': [0.25, 2.75], 'c': [1.25, 0.75]})

I want to find the mean of each column, which would return (2,1.5,1) and subtract the values from columns a,b and c respectively.

which would give, ((-0.5,0.5),(-1.25, 1.5), (0.25,-0.25))

Can anybody help me in doing this?

Thanks

Answer

The6thSense picture The6thSense · Feb 3, 2016

You could simply use the mean function of pandas

Code:

import pandas as pd
df = pd.DataFrame({'a': [1.5, 2.5], 'b': [0.25, 2.75], 'c': [1.25, 0.75]})

print "The data frame"
print df
print "The mean value"
print df.mean()
print "The value after subraction of mean"
print df -df.mean()

Output:

The data frame

    a     b     c
0  1.5  0.25  1.25
1  2.5  2.75  0.75

The mean value

a    2.0
b    1.5
c    1.0
dtype: float64

The value after subraction of mean

    a     b     c
0 -0.5 -1.25  0.25
1  0.5  1.25 -0.25