Home > database >  How can I vectorize and speed up this Pandas iterrows?
How can I vectorize and speed up this Pandas iterrows?

Time:01-10

I cannot understand how to use previous indexes within an apply() or similar.

This is the code:

for i, row in data.iterrows():

    index = data.index.get_loc(i)
    
    if index == 0:
        pass
    else:

    # changes
    data.at[i, '1_Day_%_Change'] = ( data.at[data.index[index], 'Adj_Close'] / data.at[data.index[index-1], 'Adj_Close'] ) - 1
    data.at[i, '5_Day_%_Change'] = data.at[data.index[index], 'Adj_Close'] / data.at[data.index[index-5], 'Adj_Close'] - 1
    data.at[i, '1_Month_%_Change'] = data.at[data.index[index], 'Adj_Close'] / data.at[data.index[index-21], 'Adj_Close'] - 1
    data.at[i, '6_Monthr_%_Change'] = data.at[data.index[index], 'Adj_Close'] / data.at[data.index[index-151], 'Adj_Close'] - 1
    data.at[i, '1_Year_%_Change'] = data.at[data.index[index], 'Adj_Close'] / data.at[data.index[index-252], 'Adj_Close'] - 1 

data is the dataframe, and the goal is just to make % changes for stock prices. All I am doing is dividing the current row's 'Adj Close' price by the price X rows ago.

How can I speed this up?

CodePudding user response:

Use diff and shift methods. Example code is here.

df['1_Day_%_Change'] = df['Adj_close'].diff() / df['Adj_close'].shift(1)
df['5_Day_%_Change'] = df['Adj_close'].diff(5) / df['Adj_close'].shift(5)
  • Related