title: How to apply a defined function on pandas data frame
body: <p>I have a following function defined, which is working on 2d arrays. The <code>angle</code> function is calculating the angle between vectors.  </p>
  
  <p>While calling the function below, its taking in "directions" as the parameter, which is a 2d array (with 2 cols one with x vals and another with y vals).</p>
  
  <p>Now <code>directions</code> was obtained by applying <code>np.diff()</code> function 2d array.</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  import os
  import rdp
  
  def angle(dir):
      """
      Returns the angles between vectors.
  
      Parameters:
      dir is a 2D-array of shape (N,M) representing N vectors in M-dimensional space.
  
      The return value is a 1D-array of values of shape (N-1,), with each value between 0 and pi.
  
      0 implies the vectors point in the same direction
      pi/2 implies the vectors are orthogonal
      pi implies the vectors point in opposite directions
      """
      dir2 = dir[1:]
      dir1 = dir[:-1]
      return np.arccos((dir1*dir2).sum(axis=1)/(np.sqrt((dir1**2).sum(axis=1)*(dir2**2).sum(axis=1))))
  
  tolerance = 70
  min_angle = np.pi*0.22
  
  filename = os.path.expanduser('~/tmp/bla.data')
  points = np.genfromtxt(filename).T
  print(len(points))
  x, y = points.T
  
  # Use the Ramer-Douglas-Peucker algorithm to simplify the path
  # http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
  # Python implementation: https://github.com/sebleier/RDP/
  simplified = np.array(rdp.rdp(points.tolist(), tolerance))
  
  print(len(simplified))
  sx, sy = simplified.T
  
  # compute the direction vectors on the simplified curve
  directions = np.diff(simplified, axis=0)
  theta = angle(directions)
  
  # Select the index of the points with the greatest theta
  # Large theta is associated with greatest change in direction.
  idx = np.where(theta&gt;min_angle)[0]+1
  </code></pre>
  
  <p>I want to implement the above code on a <code>pandas.DataFrame</code> with trajectory data. </p>
  
  <p>Below is the sample <code>df</code>. <code>sx</code>, <code>sy</code> values belonging to the same <code>subid</code> are considered to be one trajectory, say row(0-3) are having the same <code>subid</code> as 2, and <code>id</code> as 11 is considered to be the points of on trajectory. Rows (4-6) is one trajectory and so one. Therefore, whenever the <code>subid</code> or <code>id</code> changes, separate trajectory data is found. </p>
  
  <pre><code>  id      subid     simplified_points     sx       sy
  0 11      2         (3,4)                 3        4
  1 11      2         (5,6)                 5        6
  2 11      2         (7,8)                 7        8
  3 11      2         (9,9)                 9        9
  4 11      3         (10,12)               10       12
  5 11      3         (12,14)               12       14
  6 11      3         (13,15)               13       15
  7 12      9         (18,20)               18       20
  8 12      9         (22,24)               22       24
  9 12      9         (25,27)               25       27
  </code></pre>
  
  <p>The above data frame has been obtained after already applying the rdp algorithm. The <code>simplified_points</code> further unzipped into two columns <code>sx</code> and <code>sy</code> are the result of rdp algo.</p>
  
  <p>The problem lies in getting the <code>directions</code> for each of these trajectories and then subsequently getting <code>theta</code> and <code>idx</code>. Since the above code has been implemented only for one trajectory and that too on 2d array, I am unable to implement it for above pandas data frame.</p>
  
  <p>Please suggest me a way to implement the above code for each trajectory data in a df.</p>