Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe divide series on pandas

I need to divide each column of the matrix df1 into a single column of the matrix df2. To get a matrix with dimension df1 (3*2).

I need a result: dataframe[[1/6, 2/7, 3/8], [3/6, 4/7, 5,8]]

df1 = pd.DataFrame(data = [[1,2,3],[3,4,5]], index = ['a','b'], columns = ['i','ii','iii'])
df2 = pd.DataFrame(data = [[6],[7],[8]], index = ['a','b','c'], columns = ['i'])

df1.div(df2, axis = 'columns')
=> does not work

for i in range(0,2)
    a = df1[df1.columns[i]] / df2
=> summarizes the result in one column

Thanks for your help

like image 921
KrDan Rod Avatar asked Dec 08 '25 10:12

KrDan Rod


1 Answers

Here's one way:

pd.DataFrame(df1.values/ df2.values.T, columns=df1.columns)

       i        ii      iii
0  0.166667  0.285714  0.375
1  0.500000  0.571429  0.625
like image 87
yatu Avatar answered Dec 09 '25 22:12

yatu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!