Vichtor
Vichtor

Reputation: 197

How do I divide one row in a DataFrame with all other rows in another df?

I have two DataFrames, df1 and df2. I want to divide each row in one df with the row in my other df. When I try this by myself, I only get NaN values.

df1 has one row and three columns, and looks likes this:

    0         1         2
0 -0.001506  0.000357 -0.002386

I want to divide each row in my other DataFrame, df2, that looks like this:

0   1   2
-0.008225100913712424   0.002488360440698889    -0.008225100913712424
-0.0012552065743555163  0.002048857368006418    -0.0001238320387255687
-0.0009167397808822475  0.001411666779150167    -0.0007873838608803974
-0.0005182164342202311  0.0016391411125882271   0.001532118153346973
-0.002096707101822415   0.00020798435058289044  -0.0020292527178495945
-0.0006015392658972329  0.0006577578888782298   0.00011805910826012678
-8.441050516860837e-05  0.0009791618599572782   0.0009791618599572782
-0.000343184413801656   0.00046695584173006566  -0.00024754285585693037
-0.0003435148893994322  0.0010474388430867787   0.0009629679686444792
-0.0014786804752094929  0.0022067330755990433   0.0022067330755990433

Desired output:

4,46    6,96    3,45
-0,17   5,73    0,05
-0,39   3,95    0,33
-0,66   4,59    -0,64
0,39    0,58    0,85
-0,60   1,84    -0,05
-0,94   2,74    -0,41
-0,77   1,31    0,10
-0,77   2,93    -0,40

Upvotes: 0

Views: 46

Answers (2)

BENY
BENY

Reputation: 323226

Try

df2[:] = df2.values/df1.values
df2
Out[170]: 
          0         1         2
0  5.461554  6.970197  3.447234
1  0.833471  5.739096  0.051899
2  0.608725  3.954249  0.330002
3  0.344101  4.591432 -0.642128
4  1.392236  0.582589  0.850483
5  0.399428  1.842459 -0.049480
6  0.056049  2.742750 -0.410378
7  0.227878  1.308000  0.103748
8  0.228098  2.934002 -0.403591
9  0.981860  6.181325 -0.924867

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150735

Just do:

df2.div(df1.iloc[0])

Or

df2.div(df1.values)

Output (your output seems incorrect: -0.008225 / -0.001506 = 5.46):

          0         1         2
0  5.461554  6.970197  3.447234
1  0.833471  5.739096  0.051899
2  0.608725  3.954249  0.330002
3  0.344101  4.591432 -0.642128
4  1.392236  0.582589  0.850483
5  0.399428  1.842459 -0.049480
6  0.056049  2.742750 -0.410378
7  0.227878  1.308000  0.103748
8  0.228098  2.934002 -0.403591
9  0.981860  6.181325 -0.924867

Upvotes: 2

Related Questions