Divide two dataframes with python
14,212
Solution 1
You can use div
, but before set_index
from both columns TIMESTAMP
:
df1.set_index('TIMESTAMP', inplace=True)
df2.set_index('TIMESTAMP', inplace=True)
print (df1.div(df2).reset_index())
TIMESTAMP eq1 eq2 eq3
0 2016-05-10 13:20:00 4.0 1.5 0.333333
1 2016-05-10 13:40:00 4.0 0.5 1.000000
EDIT by comment:
df1.set_index('TIMESTAMP', inplace=True)
df2.set_index('TIMESTAMP', inplace=True)
print (df2.sum())
eq1 20
eq2 40
eq3 50
dtype: int64
print (df1.div(df2.sum()).reset_index())
TIMESTAMP eq1 eq2 eq3
0 2016-05-10 13:20:00 2.0 0.75 0.2
1 2016-05-10 13:40:00 2.0 0.25 0.4
Solution 2
This should work if TIMESTAMP
is not the index:
>>> df1.set_index('TIMESTAMP').div(df2.set_index('TIMESTAMP').sum())
eq1 eq2 eq3
TIMESTAMP
2016-05-10 13:20:00 2 0.75 0.2
2016-05-10 13:40:00 2 0.25 0.4
If TIMESTAMP
is the index, then simply this:
df1.div(df2.sum())
Related videos on Youtube
Author by
Poisson
Updated on June 04, 2022Comments
-
Poisson over 1 year
I have two dataframes :
df1
anddf2
df1
:TIMESTAMP eq1 eq2 eq3 2016-05-10 13:20:00 40 30 10 2016-05-10 13:40:00 40 10 20
df2
:TIMESTAMP eq1 eq2 eq3 2016-05-10 13:20:00 10 20 30 2016-05-10 13:40:00 10 20 20
I would like to divide
df1
bydf2
: each column ofdf1
by all column ofdf2
to get this resultdf3
:TIMESTAMP eq1 eq2 eq3 2016-05-10 13:20:00 40/(10+10) 30/(20+20) 10/(30+20) 2016-05-10 13:40:00 40/(10+10) 10/(20+20) 20/(30+20)
Any idea please?
-
Alexander about 7 yearsYes, I had assumed
TIMESTAMP
was the index. -
jezrael about 7 yearsYes, if it first columns are
indexes
, solution is simplierdf3 = df1.div(df2)
-
Poisson about 7 years@jezrael thak you for your reply, but I made an error, can you please check my post edit? Thank you
-
Poisson about 7 yearsThank you Alexander , but I made an error, can you please check my post edit? Thank you