1

I have a dataframe with 4 columns. I want to do an element-wise division of the first 3 columns by the value in 4th column

I tried:

df2 = pd.DataFrame(df.ix[:,['col1', 'col2', 'col3']].values / df.col4.values)

And I got this error:

ValueError: operands could not be broadcast together with shapes (19,3) (19,) 

My solution was:

df2 = pd.DataFrame(df.ix[:,['col1', 'col2', 'col3']].values / df.col4.values.reshape(19,1))

This worked as I wanted, but to be robust for different numbers of rows I would need to do:

.reshape(len(df),1)

It just seems an ugly way to have to do something - is there a better way around the array shape being (19,) it seems odd that it has no second dimension.

Best Regards,

Ben

BMichell
  • 3,581
  • 5
  • 23
  • 31

1 Answers1

1

You can just do div and pass axis=0 to force the division to be performed column-wise:

df2 = pd.DataFrame(df.ix[:,['col1', 'col2', 'col3']].div(df.col4, axis=0))

Your error is because the division using / is being performed on the minor axis which in this case is the row axis and there is no direct alignment, see this example:

In [220]:
df = pd.DataFrame(columns=list('abcd'), data = np.random.randn(8,4))
df

Out[220]:
          a         b         c         d
0  1.074803  0.173520  0.211027  1.357138
1  1.418757 -1.879024  0.536826  1.006160
2 -0.029716 -1.146178  0.100900 -1.035018
3  0.314665 -0.773723 -1.170653  0.648740
4 -0.179666  1.291836 -0.009614  0.392149
5  0.264599 -0.057409 -1.425638  1.024098
6 -0.106062  1.824375  0.595974  1.167115
7  0.601544 -1.237881  0.106854 -1.276829

In [221]:
df.ix[:,['a', 'b', 'c']]/df['d']

Out[221]:
    a   b   c   0   1   2   3   4   5   6   7
0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
6 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
7 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

This isn't obvious until you understand how broadcasting works.

Community
  • 1
  • 1
EdChum
  • 376,765
  • 198
  • 813
  • 562