Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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) and df.divide(df2, axis='index') and multiple other solutions and I always get a df with nan values in every cell. What argument am I missing in the function df.divide?

like image 856
Plug4 Avatar asked Mar 25 '14 17:03

Plug4


2 Answers

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

import pandas as pd  data1 = {"a":[1.,3.,5.,2.],          "b":[4.,8.,3.,7.],          "c":[5.,45.,67.,34]} data2 = {"a":[4.],          "b":[2.],          "c":[11.]}  df1 = pd.DataFrame(data1) df2 = pd.DataFrame(data2)   df1.div(df2.iloc[0], axis='columns') 

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

like image 185
kimal Avatar answered Sep 29 '22 12:09

kimal


You can divide by the series i.e. the first row of df2:

In [11]: df = pd.DataFrame([[1., 2.], [3., 4.]], columns=['A', 'B'])  In [12]: df2 = pd.DataFrame([[5., 10.]], columns=['A', 'B'])  In [13]: df.div(df2) Out[13]:       A    B 0  0.2  0.2 1  NaN  NaN  In [14]: df.div(df2.iloc[0]) Out[14]:       A    B 0  0.2  0.2 1  0.6  0.4 
like image 26
Andy Hayden Avatar answered Sep 29 '22 12:09

Andy Hayden