15

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

JejeBelfort
  • 1,593
  • 2
  • 18
  • 39
haimen
  • 1,985
  • 7
  • 30
  • 53

2 Answers2

22

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
The6thSense
  • 8,103
  • 8
  • 31
  • 65
4

try this:

>>> df
     a     b     c
0  1.5  0.25  1.25
1  2.5  2.75  0.75
>>> df.columns
Index([u'a', u'b', u'c'], dtype='object')
>>> for x in df.columns:
...     df[x] = df[x] - df[x].mean()
... 
>>> df
     a     b     c
0 -0.5 -1.25  0.25
1  0.5  1.25 -0.25

Pythonic way:

>>> df - df.mean()
     a     b     c
0 -0.5 -1.25  0.25
1  0.5  1.25 -0.25
Hackaholic
  • 19,069
  • 5
  • 54
  • 72