Vectorized look-up of values in Pandas dataframe
Use our friend lookup
, designed precisely for this purpose:
In [17]: pricesOut[17]: AAPL GOOG IBM XOM2011-01-10 339.44 614.21 142.78 71.572011-01-13 342.64 616.69 143.92 73.082011-01-26 340.82 616.50 155.74 75.892011-02-02 341.29 612.00 157.93 79.462011-02-10 351.42 616.44 159.32 79.682011-03-03 356.40 609.56 158.73 82.192011-05-03 345.14 533.89 167.84 82.002011-06-03 340.42 523.08 160.97 78.192011-06-10 323.03 509.51 159.14 76.842011-08-01 393.26 606.77 176.28 76.672011-12-20 392.46 630.37 184.14 79.97In [18]: ordersOut[18]: Date direction size ticker prices0 2011-01-10 00:00:00 Buy 1500 AAPL 339.441 2011-01-13 00:00:00 Sell 1500 AAPL 342.642 2011-01-13 00:00:00 Buy 4000 IBM 143.923 2011-01-26 00:00:00 Buy 1000 GOOG 616.504 2011-02-02 00:00:00 Sell 4000 XOM 79.465 2011-02-10 00:00:00 Buy 4000 XOM 79.686 2011-03-03 00:00:00 Sell 1000 GOOG 609.567 2011-03-03 00:00:00 Sell 2200 IBM 158.738 2011-06-03 00:00:00 Sell 3300 IBM 160.979 2011-05-03 00:00:00 Buy 1500 IBM 167.8410 2011-06-10 00:00:00 Buy 1200 AAPL 323.0311 2011-08-01 00:00:00 Buy 55 GOOG 606.7712 2011-08-01 00:00:00 Sell 55 GOOG 606.7713 2011-12-20 00:00:00 Sell 1200 AAPL 392.46In [19]: prices.lookup(orders.Date, orders.ticker)Out[19]: array([ 339.44, 342.64, 143.92, 616.5 , 79.46, 79.68, 609.56, 158.73, 160.97, 167.84, 323.03, 606.77, 606.77, 392.46])