Most efficient row multiplication with matrix in Pandas
<p>Suppose I have a matrix as such </p>

<pre><code>df = pd.DataFrame(randint(2,size=(3,9)))
df.values
array([[0, 1, 0, 1, 1, 1, 0, 1, 1],
       [1, 0, 1, 1, 1, 1, 0, 0, 1],
       [0, 0, 0, 1, 0, 0, 1, 1, 0]])
</code></pre>

<p>Again; each row in this example represents three 3D coordinates, that need to rotated by, e.g. the following rotation matrix:</p>

<pre><code>array([[  0.00000000e+00,   0.00000000e+00,   1.00000000e+00],
       [  0.00000000e+00,   1.00000000e+00,   0.00000000e+00],
       [ -1.00000000e+00,   0.00000000e+00,   0.00000000e+00]])
</code></pre>

<p>To do this as efficiently as possible (the real problem has millions of coordinates btw) currently, I am somewhat baffled that I have to do:</p>

<p>First apply <code>df.reshape</code> - each row in this example consists of three 3D coordinates as so [(x,y,z),(x,y,z),(x,y,z)]:</p>

<pre><code>array([[0, 1, 0],
       [1, 1, 1],
       [0, 1, 1],
       [1, 0, 1],
       [1, 1, 1],
       [0, 0, 1],
       [0, 0, 0],
       [1, 0, 0],
       [1, 1, 0]])
</code></pre>

<p>Then in order to get it to <a href="https://en.wikipedia.org/wiki/Rotation_matrix" rel="nofollow">rotate to convention</a>, one must take <code>u_new = R \dot u</code> which means the transpose of the above, so that we can take a column-wise (i.e. coordinate) multiplication with the rotation matrix.</p>

<pre><code>array([[0, 1, 0, 1, 1, 0, 0, 1, 1],
       [1, 1, 1, 0, 1, 0, 0, 0, 1],
       [0, 1, 1, 1, 1, 1, 0, 0, 0]])
</code></pre>

<p>Then we can do the multiplication:</p>

<pre><code>pd.DataFrame(dot(rotmat,df)).values

array([[  0.00e+00,   2.22e-16,   0.00e+00,   1.00e+00,   2.22e-16,
          2.22e-16,   1.00e+00,   1.00e+00,   2.22e-16],
       [  1.00e+00,   0.00e+00,   1.00e+00,   1.00e+00,   1.00e+00,
          1.00e+00,   0.00e+00,   0.00e+00,   1.00e+00],
       [  0.00e+00,  -1.00e+00,   0.00e+00,  -1.00e+00,  -1.00e+00,
         -1.00e+00,   2.22e-16,  -1.00e+00,  -1.00e+00]])
</code></pre>

<p>And then reverse the whole process to get this back into the original shape, to be used for other purposes. </p>

<p>Surely there must be more efficient ways to do this (hopefully without messing with the rotation matrix)?</p>

<p>This should never touch a dataframe until you are done with your transformations.</p>

<pre><code>a = np.array([
        [0, 1, 0, 1, 1, 1, 0, 1, 1],
        [1, 0, 1, 1, 1, 1, 0, 0, 1],
        [0, 0, 0, 1, 0, 0, 1, 1, 0]
    ])

rotmat = np.array([
        [  0.00000000e+00,   0.00000000e+00,   1.00000000e+00],
        [  0.00000000e+00,   1.00000000e+00,   0.00000000e+00],
        [ -1.00000000e+00,   0.00000000e+00,   0.00000000e+00]
    ])

a.reshape(3, 3, -1).dot(rotmat).reshape(-1, 9)

array([[ 0.,  1.,  0., -1.,  1.,  1., -1.,  1.,  0.],
       [-1.,  0.,  1., -1.,  1.,  1., -1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  1.,  1.]])
</code></pre>

<hr>

<pre><code>df = pd.DataFrame(a.reshape(3, 3, -1).dot(rotmat).reshape(-1, 9))
df
</code></pre>

<p><a href="http://i.stack.imgur.com/JGP9t.png" rel="nofollow"><img src="http://i.stack.imgur.com/JGP9t.png" alt="enter image description here"></a></p>

python pandas numpy matrix