title: Adding calculated column(s) to a dataframe in pandas
body: <p>I have an OHLC price data set, that I have parsed from CSV into a Pandas dataframe and resampled to 15 min bars:</p>
  
  <pre><code>&lt;class 'pandas.core.frame.DataFrame'&gt;
  DatetimeIndex: 500047 entries, 1998-05-04 04:45:00 to 2012-08-07 00:15:00
  Freq: 15T
  Data columns:
  Close    363152  non-null values
  High     363152  non-null values
  Low      363152  non-null values
  Open     363152  non-null values
  dtypes: float64(4)
  </code></pre>
  
  <p>I would like to add various calculated columns, starting with simple ones such as period Range (H-L) and then booleans to indicate the occurrence of price patterns that I will define - e.g. a hammer candle pattern, for which a sample definition:</p>
  
  <pre><code>def closed_in_top_half_of_range(h,l,c):
      return c &gt; l + (h-l)/2
  
  def lower_wick(o,l,c):
      return min(o,c)-l
  
  def real_body(o,c):
      return abs(c-o)
  
  def lower_wick_at_least_twice_real_body(o,l,c):
      return lower_wick(o,l,c) &gt;= 2 * real_body(o,c)
  
  def is_hammer(row):
      return lower_wick_at_least_twice_real_body(row["Open"],row["Low"],row["Close"]) \
      and closed_in_top_half_of_range(row["High"],row["Low"],row["Close"])
  </code></pre>
  
  <p>Basic problem: how do I map the function to the column, specifically where I would like to reference more than one other column or the whole row or whatever? </p>
  
  <p><a href="https://stackoverflow.com/questions/12356501/pandas-create-two-new-columns-in-a-dataframe-with-values-calculated-from-a-pre">This post</a> deals with adding two calculated columns off of a single source column, which is close, but not quite it.</p>
  
  <p>And slightly more advanced: for price patterns that are determined with reference to more than a single bar (T), how can I reference different rows (e.g. T-1, T-2 etc.) from within the function definition?</p>