I have a dataframe df_orders such as this:
Symbol action orderid qty status price
AAPL buy 1232131 100 open 110.41
MSFT sell 8120472 200 open 83.42
MSFT sell 8120473 500 open 81.12
I want to get orderid, qty, status and price for each row where 'Symbol' == 'MSFT' and 'Action' == 'sell' and put them into an array. Desired output is
[['MSFT','sell',8120472,200,'open',83.42],['MSFT','sell',8120473,200,'open',81.12]]
I wrote the following code a while ago that works, but it is not efficient. I need to run this piece of code at least 50 million times per day and a very millisecond counts.
myarray = []
dfsells = df_orders[df_orders['Symbol'] == symbol]
if len(dfsells.index) > 0:
dfsells = dfsells[dfsells['action'] == 'Sell']
dfsells = dfsells.reset_index(drop=True)
if len(dfsells.index) > 0:
for j in range(0,len(dfsells.index)):
orderid = dfsells.loc[j,'orderid']
sellqty = dfsells.loc[j,'qty']
status = dfsells.loc[j,'status']
price = round(float(dfsells.loc[j,'price']),2)
myarray.append([symbol,'sell',orderid,sellqty,status,price])
What is the quickest way to get this array?
df.loc[(df.Symbol=='MSFT' | df.Action=='sell, [list of features you like].values