1

I have a dataframe with two columns, x and y, and a few hundred rows. I have another dataframe with only one row and two columns, x and y.

I want to divide column x of the big dataframe by the value in x of the small dataframe, and column y by column y.

If I divide one dataframe by the other, I get all NaNs. For the division to work, I must convert the small dataframe to numpy.

Why can't I divide one dataframe by the other? What am I missing? I have a toy example below.

import numpy as np
import pandas as pd

df = pd.DataFrame()
r = int(10)
df['x'] = np.arange(0,r)
df['y'] = df['x'] * 2

other_df = pd.DataFrame()
other_df['x'] = [100]
other_df['y'] = [400]

# This doesn't work - I get all nans
new = df / other_df

# this works - it gives me what I want
new2 = df / [100,400]

# this also works
new3 = df / other_df.to_numpy()
halfer
  • 19,824
  • 17
  • 99
  • 186
Pythonista anonymous
  • 8,140
  • 20
  • 70
  • 112

2 Answers2

2

You can convert one row DataFrame to Series for correct align columns, e.g. by selecting first row by DataFrame.iloc:

new = df / other_df.iloc[0]
print (new)
      x      y
0  0.00  0.000
1  0.01  0.005
2  0.02  0.010
3  0.03  0.015
4  0.04  0.020
5  0.05  0.025
6  0.06  0.030
7  0.07  0.035
8  0.08  0.040
9  0.09  0.045
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks, but why is that? Why does pandas not like dividing by a 1x2 dataframe but is fine dividing by a list, a numpy array or a series? – Pythonista anonymous Feb 11 '20 at 14:49
  • 1
    @Pythonistaanonymous - Because alignment - it means if want divide 2 DataFrames pandas try align index and columns values between both, and then divide. So here match ony first row, because here is intersection of first index values `0` in both DataFrames. Also columns names are same, so no problem. – jezrael Feb 11 '20 at 14:51
  • If convert to Series there is only align between index of Series and columns names, here `x, y` – jezrael Feb 11 '20 at 14:52
0

You can use numpy.divide() to divide as numpy has a great property that is Broadcasting.

new = np.divide(df,other_df)

Please check this link for more details.