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 [23]: A = numpy.arange(9)
In [24]: B = A.reshape((3,3))
In [25]: C = numpy.median(B,axis=0)
In [26]: D = B - C[None,:]
In [27]: B
Out[27]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [28]: D
Out[28]:
array([[-3., -3., -3.],
[ 0., 0., 0.],
[ 3., 3., 3.]])
In [29]: C
Out[29]: 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,:]

Related Links

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

Categories

HOME
jsf
mql4
signalr
redis
elixir
nlp
amazon-swf
ipython
backup
json-ld
rfc
laravel-4
xtext
ext.net
bro
schemacrawler
google-plus
vuex
cartodb
pjsip
outlook-web-addins
xlsx
uiscrollview
activesync
postmessage
line-api
session-timeout
internet-explorer-8
zend-framework-mvc
scalaz7
servicemix
ggmap
dpi
thinking-sphinx
hibernate-cache
jackson-dataformat-csv
construct-2
ifstream
pycparser
body-parser
node-horseman
reactivemongo
css-counter
nunit-3.0
agent
delphi-10.1-berlin
intersystems-ensemble
powermta
pytest-django
rkt
facebook-chatbot
startapp
amazon-clouddrive
estimation
lirc
nsjsonserialization
sapui
cgcontext
decompress
modelattribute
in-memory-database
infinite-scroll
visual-studio-templates
ebtables
cleditor
gradle-eclipse
supportmapfragment
asp.net-mvc-2
ninject.web.mvc
rad
anti-patterns
achartengine
team-build
twitter-rest-api
bridge
epl
searchkick
temp-tables
and-operator
x-ua-compatible
mov
funq
mfmailcomposeviewcontroll
broadcasting
visual-c++-2010-express
baucis
real-time-updates
nimrod
trimming
client-side-scripting
fortran77
awesomeprint
android-contextmenu
marmalade-edk
mvccontrib
usn
couchdb-lucene
radchart
redirectstandardoutput
expression-evaluation
throttling
adsl
adobe-contribute
chatroom
carbide
savestate
kpi

Resources

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