From a DataFrame type:
>>> df Date Open High Low Close Volume Adj Close 0 2013-07-08 76.91 77.81 76.85 77.04 5106200 77.04 1 2013-07-00 77.04 79.81 71.81 72.87 1920834 77.04 2 2013-07-10 72.87 99.81 64.23 93.23 2934843 77.04
The simplest thing I can think of will be:
>>> df["Test"] = df[["Open", "Close"]].max(axis=1) >>> df Date Open High Low Close Volume Adj Close Test 0 2013-07-08 76.91 77.81 76.85 77.04 5106200 77.04 77.04 1 2013-07-00 77.04 79.81 71.81 72.87 1920834 77.04 77.04 2 2013-07-10 72.87 99.81 64.23 93.23 2934843 77.04 93.23
df.ix[:,["Open", "Close"]].max(axis=1) might be a little faster, but I don't think it's nice to watch.
Alternatively, you can use .apply for strings:
>>> df["Test"] = df.apply(lambda row: max(row["Open"], row["Close"]), axis=1) >>> df Date Open High Low Close Volume Adj Close Test 0 2013-07-08 76.91 77.81 76.85 77.04 5106200 77.04 77.04 1 2013-07-00 77.04 79.81 71.81 72.87 1920834 77.04 77.04 2 2013-07-10 72.87 99.81 64.23 93.23 2934843 77.04 93.23
Or go back to numpy:
>>> df["Test"] = np.maximum(df["Open"], df["Close"]) >>> df Date Open High Low Close Volume Adj Close Test 0 2013-07-08 76.91 77.81 76.85 77.04 5106200 77.04 77.04 1 2013-07-00 77.04 79.81 71.81 72.87 1920834 77.04 77.04 2 2013-07-10 72.87 99.81 64.23 93.23 2934843 77.04 93.23
The main problem is that if/else does not play well with arrays, because if (something) always forces something to a single bool . This is not equivalent to "for each element in the array something if the condition is met" or something like that.
source share