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
To calculate the mean of whole columns in the DataFrame, use pandas. Series. mean() with a list of DataFrame columns. You can also get the mean for all numeric columns using DataFrame.
subtract() function is used for finding the subtraction of dataframe and other, element-wise. This function is essentially same as doing dataframe – other but with a support to substitute for missing data in one of the inputs.
Difference between rows or columns of a pandas DataFrame object is found using the diff() method. The axis parameter decides whether difference to be calculated is between rows or between columns. When the periods parameter assumes positive values, difference is found by subtracting the previous row from the next row.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With