0

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
8
  • What do you get as output for print(df1.shape, df2.shape)? Commented Oct 9, 2019 at 0:14
  • df1 = (107,7) df2 = (107,7) Commented Oct 9, 2019 at 0:19
  • And over df1.dtypes and df2.dtypes. Do you get object? Or int and float? Commented Oct 9, 2019 at 0:21
  • Then df1 / df2 should just work, what do you get as output when you try that code? Commented Oct 9, 2019 at 0:24
  • 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? Commented Oct 9, 2019 at 0:26

2 Answers 2

0

You can try the following code:

df3 = df2.div(df1.iloc[0], axis='columns')
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.