I am trying to divide rows of a dataframe by the same index row in another dataframe. There are the same amount of columns in each dataframe.
The goal is to divide a list of columns by another list of columns. Is there a way to do this in Pandas?
Here is a sample data:
import pandas as pd
import numpy as np
data1 = {"a":[10.,20.,30.,40.,50.],
"b":[900.,800.,700.,600.,500.],
"c":[2.,4.,6.,8.,10.]}
data2 = {"f":[1.,2.,3.,4.],
"g":[900.,800.,700.,600.],
"h":[10.,20.,30.,40.]}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
Expected output:
a/f b/g c/h
0 10.0 1.0 0.2
1 10.0 1.0 0.2
2 10.0 1.0 0.2
3 10.0 1.0 0.2
4 NaN NaN NaN
As of now, I am using this little function I wrote:
def divDF(df1, df2):
nRow, nCol = df1.shape
result = pd.DataFrame(np.empty((nRow, nCol)), index=df1.index)
for col in range(nCol):
result.iloc[:,col] = df1.iloc[:,col] / df2.iloc[:,col]
return result
Is this the only way or is there a faster way of doing this?
divide by values to get around index alignment
dfd = df1.div(df2.values)
dfd.columns = df1.columns + '/' + df2.columns
dfd
a/f b/g c/h
0 10.0 1.0 0.2
1 10.0 1.0 0.2
2 10.0 1.0 0.2
3 10.0 1.0 0.2
Or
c = df1.columns + '/' + df2.columns
pd.DataFrame(df1.values / df2.values, df1.index, c)
a/f b/g c/h
0 10.0 1.0 0.2
1 10.0 1.0 0.2
2 10.0 1.0 0.2
3 10.0 1.0 0.2
rebirth of @ScottBoston's answer
c = df1.columns + '/' + df2.columns
d1 = dict(zip(df1.columns, c))
d2 = dict(zip(df2.columns, c))
df1.rename(columns=d1) / df2.rename(columns=d2)
a/f b/g c/h
0 10.0 1.0 0.2
1 10.0 1.0 0.2
2 10.0 1.0 0.2
3 10.0 1.0 0.2
4 NaN NaN NaN
Using align
to force index alignment:
df3 = np.divide(*df1.align(df2, axis=0))
df3.columns = df1.columns + '/' + df2.columns
The resulting output:
a/f b/g c/h
0 10.0 1.0 0.2
1 10.0 1.0 0.2
2 10.0 1.0 0.2
3 10.0 1.0 0.2
4 NaN NaN NaN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With