Plug4 Plug4 - 1 year ago 175
Python Question

Python: Divide each row of a DataFrame by another DataFrame vector

I have a DataFrame (df1) with a dimension

2000 rows x 500 columns
(excluding the index) for which I want to divide each row by another DataFrame (df2) with dimension
1 rows X 500 columns
. Both have the same column headers. I tried:

df.divide(df2, axis='index')
and multiple other solutions and I always get a df with
values in every cell. What argument am I missing in the function

Answer Source

In df.divide(df2, axis='index'), you need to provide the axis/row of df2 (ex. df2.ix[0]).

import pandas as pd

data1 = {"a":[1.,3.,5.,2.],
data2 = {"a":[4.],

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2) 


or you can use df1/df2.values[0,:]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download