pandas


How to divide the value of pandas columns by the other column


I have a dataframe:
>>> dt
COL000 COL001 QT
STK_ID RPT_Date
STK000 20120331 2.6151 2.1467 1
20120630 4.0589 2.3442 2
20120930 4.4547 3.9204 3
20121231 4.1360 3.8559 4
STK001 20120331 -0.2178 0.9184 1
20120630 -1.9639 0.7900 2
20120930 -2.9147 1.0189 3
20121231 -2.5648 2.3743 4
STK002 20120331 -0.6426 0.9543 1
20120630 -0.3575 1.6085 2
20120930 -2.3549 0.7174 3
20121231 -3.4860 1.6324 4
And I want the columns values divided by 'QT' column, somewhat like this:
dt = dt/dt.QT # pandas does not accept this syntax
The desired output is:
STK_ID RPT_Date COL000 COL001 QT
STK000 20120331 2.615110188 2.146655745 1
20120630 2.029447265 1.172093561 1
20120930 1.484909881 1.306795608 1
20121231 1.034008443 0.963970609 1
STK001 20120331 -0.217808111 0.918355842 1
20120630 -0.981974837 0.394977675 1
20120930 -0.97157148 0.339633733 1
20121231 -0.641203355 0.593569537 1
STK002 20120331 -0.642567516 0.954323016 1
20120630 -0.178759288 0.804230898 1
20120930 -0.784982521 0.239117442 1
20121231 -0.871501505 0.408094317 1
How to do that?
The / operator for dv seems equal to div with default axis "columns". Set the axis to "index", then it'll work.
df = df.div(df.QT, axis='index')
Another tricky way is to transpose it first, divide it, and then transpose back:
df = (df.T / df.QT).T

Related Links

Why I can't change the series format?
Copy numpy array into Panda multiindex (same size)
Average Previous and Current Row - Pandas
seaborn pointplot above swarmplot
Edit field and append value to a python dataframe column
column_stack returns non cotiguous array
pandas: conditionally select a row cell for each column based on a mask
pandas custom function apply on melted dataframe
How to check for boolean codition in pandas dataframe
Reading batches of data from BigQuery into Datalab
Jupyter/ipywidgets sorting dataframe on two levels
Groupby.sum() giving ValueError: overflow in timedelta operation
Why does DataFrameGroupBy.boxplot method throw error when given argument “subplots=True/False”?
Calculate age in months - optimize date transformations in pandas
pandas: list of dictionaries grouped by key from df
Pandas data frames and matplotlib.pyplot

Categories

HOME
compiler-construction
coq
mediawiki
tizen
pycharm
push-notification
mean-stack
programming-languages
graphql
jsrender
google-project-tango
add
gorm
here-api
mapserver
circular-dependency
hex-editors
quickfix
decomposition
flux
jqwidget
text-rendering
kryo
xlsxwriter
scriptcs
tar
mustache.php
core-text
language-agnostic
mmap
numerical-methods
info.plist
objectanimator
primitive
gsoap
microsoft-chart-controls
sql-server-2012-express
google-sites-2016
column-family
hexo
bower-install
http-digest
espeak
cookiecutter-django
graphenedb
email-templates
fedex
bootstrap-dialog
segment
pubmed
paxos
pebble-watch
tizen-native-app
darcs
sqlbulkcopy
dwscript
sonarlint-vs
orthogonal
crosswalk-runtime
parallel-data-warehouse
xcb
deadbolt-2
bonobo
elements
pcf
multiple-regression
itextpdf
associative-array
jms2
lemon
sdhc
python-3.2
varargs
createprocessasuser
oxwall
coldbox
titanium-modules
reactfx
doskey
mysqltuner
inbox
dotnetnuke-5
batterylevel
shellexecute
orchardcms-1.7
distutils
bitsharp
nsmanagedobject
xmemcached
disclosure
imac
paster
gamequery
icanhaz.js
mozilla-prism
thunderbird-lightning
iweb
microsoft-virtualization
photoshop-cs4
port-number
mediarss

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