-1

I have a data frame with positional data stored like this:

    r1_x        r1_y        r1_z        r2_x        r2_y        r2_z
0   17.670965   19.857307   27.178185   17.555960   20.653801   27.641266
1   17.670965   19.857307   27.178185   16.950911   4.216056    12.901429
2   17.670965   19.857307   27.178185   11.737829   26.097181   3.230895
3   17.670965   19.857307   27.178185   16.254911   12.062716   12.170364
4   17.670965   19.857307   27.178185   16.504757   11.673612   12.406663
5   17.670965   19.857307   27.178185   16.717722   27.935490   28.795826

Here the x, y, and z denote the components of a 3d vector. What is the best way to go about performing vector operations on such a data frame element wise? For example, what if I wanted to calculate r2-r1 for each row, or take a dot product? I am using pandas to store this data.

aydow
  • 3,673
  • 2
  • 23
  • 40
Andrew King
  • 145
  • 10
  • If you want `r1_x` - `r2_x` just do `df.r1_x - df.r2_x` – rafaelc Jul 22 '18 at 23:59
  • I’m sorry if I wasn’t clear, I know how to do operations on each column. My question is how do I combine the 3 columns x,y, and z into a numpy array for each row so that I can do things like np.dot(r1, r2) – Andrew King Jul 23 '18 at 01:47
  • I’m looking for a more efficient way to do it than having to manipulate each component of these vectors. – Andrew King Jul 23 '18 at 01:55
  • define `efficient`? You want more performance, is that it? Or a nicer code? Because it won't go any much faster than `(df.r1_x * df.r2_x) + (df.r1_y * df.r2_y) + (df.r1_z * df.r2_z)` – rafaelc Jul 23 '18 at 01:57

1 Answers1

0

Try this

df['r2_x-r1_x'] = df.r2_x - df.r1_x
aydow
  • 3,673
  • 2
  • 23
  • 40
  • It was marked duplicate at just about the same time as you posted your answer. Also to help future users, you should explain your code. – Nic3500 Jul 23 '18 at 00:35