new column based on a condition of the others columns (like ifelse in R)
Hi everyone,

How can I do a ifelse statement R-like in a pandas dataframe.

I want to compute a new column via a calculation from the others columns.
It's been a while, so I ask to the community. I used to do a simple ifelse(cond, true, false) with R
on a dataframe.I look for the same result.

here is my code:

import pandas.io.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
f=web.DataReader("F", 'yahoo', start, end)

here is a print of the first 5 lines:
             Open   High    Low  Close     Volume  Adj Close
Date  
2010-01-04  10.17  10.28  10.05  10.28   60855800       9.43
2010-01-05  10.45  11.24  10.40  10.96  215620200      10.05
2010-01-06  11.21  11.46  11.13  11.37  200070600      10.43
2010-01-07  11.46  11.69  11.32  11.66  130201700      10.69
2010-01-08  11.67  11.74  11.46  11.69  130463000      10.72

I try to calculate a new columns based on the others:

f["new column"] = if open  > close then (high - open)
                  if close >= open  then (high - close)
                  else nan
 I just started to test the applymap function by doing this:
 f.applymap(lambda x: x["open"]*2)

 it gave me that error:
 IndexError: ('invalid index to scalar variable.', u'occurred at index Open')

 Anyone has a solution ?

 regards,

 mayouf
