简体   繁体   English

我如何矢量化和加速这个 Pandas iterrows?

[英]How can I vectorize and speed up this Pandas iterrows?

I cannot understand how to use previous indexes within an apply() or similar.我不明白如何在 apply() 或类似的方法中使用以前的索引。

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.数据是 dataframe,目标只是对股票价格进行百分比变化。 All I am doing is dividing the current row's 'Adj Close' price by the price X rows ago.我所做的只是将当前行的“Adj Close”价格除以 X 行前的价格。

How can I speed this up?我怎样才能加快速度?

Use diff and shift methods.使用diffshift方法。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM