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,:]
What is the difference between isinstance('aaa', basestring) and isinstance('aaa', str)?
Is this essential functional programming feature missing from python?
Hooking into a wave-out on different platforms
What causes subprocess.call to output blank file when attempting db export with mysqldump?
How do I remove something form a list, plus string matching?
Reference to Part of List - Python
Django Delete all but last five of queryset
How do I limit the number of active threads in python?
Calculating the pixel size of a string with Python
Python nested lists and recursion problem
Problems PUTting binary data to Django
How to use long integers in Python to build a range?
Should I use Lex or a home-brewed solution to parse a formula?
Launching default application for given type of file, OS X
Python CGI script IOError Broken Pipe
Django, grouping query items