python

Deducting the median from each column

```I have a dataframe, df with numbers, like so:
1 1 1
2 1 1
2 1 3
I'd like to deduct the median from each column so that the median of each becomes 0.
-1 0 0
0 0 0
0 0 2
How do I do this in a pythandic way? I'm guessing it is possible without iterating over the values, computing the median and then deducting. I'd like to do it tersely, approximately like so:
from numpy import median
df -= median(df) #does not work, deducts median for whole dataframe
```
```Just like this
df -= df.median(axis=0)
median of numpy computes median of overall data.
To accomplish using numpy, try this code instead.
df -= median(df, axis=0)
for more detail, see the document: http://docs.scipy.org/doc/numpy/reference/generated/numpy.median.html
```
```Some testing in ipython showed:
In : A = numpy.arange(9)
In : B = A.reshape((3,3))
In : C = numpy.median(B,axis=0)
In : D = B - C[None,:]
In : B
Out:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In : D
Out:
array([[-3., -3., -3.],
[ 0., 0., 0.],
[ 3., 3., 3.]])
In : C
Out: array([ 3., 4., 5.])
So the next line gets the median along the columns
C = numpy.median(B,axis=0)
And the next line subtracts it from the matrix, column by column
D = B - C[None,:]```

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization