0

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?

1
  • you can just write df.loc[(df.Symbol=='MSFT' | df.Action=='sell, [list of features you like].values Commented Nov 16, 2020 at 20:32

1 Answer 1

1
df[(df['Symbol'] == symbol) & (df['action'] == 'Sell')].values.tolist()

Test case (in notebook):

n = 10000000
df = pd.DataFrame(
    {
        'Symbol': np.random.randint(0, 10, n),        
        'action': np.random.randint(0, 10, n),  
        'orderid': np.random.randint(0, 10, n),  
        'qty': np.random.randint(0, 10, n),  
        'status': np.random.randint(0, 10, n),  
        'price': np.random.randint(0, 10, n),  
    }
)

%%time
len(df[(df['Symbol'] == 3) & (df['action'] == 4)].values.tolist())

Output:

Wall time: 68.6 ms
99781

It took 68.6ms so pretty fast I guess.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.