Python – Use if conditions in Python to speed up progressive loops

Use if conditions in Python to speed up progressive loops… here is a solution to the problem.

Use if conditions in Python to speed up progressive loops

I have a dataset of 6 million rows with columns: symbol, timeStamp, open price, and close price. I run the following loop, which is very simple, but takes a long time (if open price is nan, get close price from the previous line):

for i in range(0,len(price2)):
    print(i)
    if np.isnan(price3.iloc[i,2]):
        price3.iloc[i,2]=price3.iloc[i-1,3]

How can I speed up this cycle? As far as I know, I can change to apply(), but how can I include an if condition in it?

Solution

You can use >pandas. Series.fillna instead of a transfer series for loops with closing prices.

price3['open price'].fillna(price3['close price'].shift(1), inplace=True)

This is vectorized, so it should be much faster than your for loop.

Note that I assume price2 and price3 have the same length, and you can also iterate price3 in a loop.

Related Problems and Solutions