0
votes

I have two identical sized dataframes (df1 & df2). I would like to create a new dataframe with values that are df1 column1 / df2 column1. So essentially df3 = df1(c1)/df2(c1), df1(c2)/df2(c2), df1(c3)/df2(c3)...

I've tried the below code, however both give a dataframe filled with NaN

#attempt 1
df3 = df2.divide(df1, axis='columns')
#attempt 2
df3= df2/df1
2
What do you get as output for print(df1.shape, df2.shape)?Erfan
df1 = (107,7) df2 = (107,7)lsama
And over df1.dtypes and df2.dtypes. Do you get object? Or int and float?Erfan
Then df1 / df2 should just work, what do you get as output when you try that code?Erfan
a whole bunch of NaNs? Could it be cause df1 and df2 have occasional NaN values? In this situation of df1(r1c1) = NaN , df2(r1c2)=17. I would have thought df3(r1c1) would equal NaN?lsama

2 Answers

0
votes

You can try the following code:

df3 = df2.div(df1.iloc[0], axis='columns')
0
votes

To use the divide function, the indexes of the dataframes need to match. In this situation, df1 was beginning of month values, df2 was end of month. The questions can be solved by:

df3 = df2.reset_index(drop=True)/df1.reset_index(drop=True)
df3.set_index(df2.index,inplace=True) ##set the index back to the original (ie end of month)