Compute 5 different rolling-means of each stock in large df

961 views Asked by At

I have a Dataframe with stock prices. Example below but this goes on for 4500 lines of stock prices

>>
DATE        MMM     US Equity   AIR     US Equity
1/3/2000    47.19               17.56
1/4/2000    45.31               17.63
1/5/2000    46.63               17.81
1/6/2000    50.38               17.94

I have created moving average using iteritems with the follow

>>>for stockname, stock in df.iteritems():     
# Create 10,30,50,100 and 200D MAvgs                             
MA10D = stock.rolling(10).mean()
MA30D = stock.rolling(30).mean()
MA50D = stock.rolling(50).mean()
MA100D = stock.rolling(100).mean()
MA200D = stock.rolling(200).mean()
df_stockname = pd.concat([df[[1]],MA10D,MA30D,MA50D,MA100D,MA200D],axis=1)

The problem is this only shows the last item in the loop (the AIR US Equity stock). How do I access MA10D, MA30D, etc for the first stock in the loop (ie the MMM US Equity which is the first stock in the df). How can I do this

I ultimately want to be able to create two data frames, one for each stock with the stock price, MA10D, MA30D, MA50D, MA100D and MA200D. So I ultimate need a way to name each dataframe and change the df[[#]] within the concat.

1

There are 1 answers

2
Bill On

Is this what you are trying to do?

results = {}

# Create 10,30,50,100 and 200D MAvgs                             
for stockname, stock in df.iteritems():
    df_copy = pd.DataFrame(stock)
    df_copy[stockname + '_MA10D'] = stock.rolling(10).mean()
    df_copy[stockname + '_MA30D'] = stock.rolling(30).mean()
    df_copy[stockname + '_MA50D'] = stock.rolling(50).mean()
    df_copy[stockname + '_MA100D'] = stock.rolling(100).mean()
    df_copy[stockname + '_MA200D'] = stock.rolling(200).mean()
    results[stockname] = df_copy

Here is a complete version that should execute and the results:

data = {
    'MMM': (47.19, 45.31, 46.63, 50.38),
    'AIR': (17.56, 17.63, 17.81, 17.94)
}
index = pd.Index(pd.date_range("01/03/2000", "01/06/2000"), name='DATE')
df = pd.DataFrame(data=data, index=index)

results = {}

# Create 10,30,50,100 and 200D MAvgs                             
for stockname, stock in df.iteritems():
    df_copy = pd.DataFrame(stock)
    df_copy[stockname + '_MA10D'] = stock.rolling(10).mean()
    df_copy[stockname + '_MA30D'] = stock.rolling(30).mean()
    df_copy[stockname + '_MA50D'] = stock.rolling(50).mean()
    df_copy[stockname + '_MA100D'] = stock.rolling(100).mean()
    df_copy[stockname + '_MA200D'] = stock.rolling(200).mean()
    results[stockname] = df_copy

print(results['MMM'])
print(results['AIR'])

Output:

              MMM  MMM_MA10D  MMM_MA30D  MMM_MA50D  MMM_MA100D  MMM_MA200D
DATE                                                                      
2000-01-03  47.19        NaN        NaN        NaN         NaN         NaN
2000-01-04  45.31        NaN        NaN        NaN         NaN         NaN
2000-01-05  46.63        NaN        NaN        NaN         NaN         NaN
2000-01-06  50.38        NaN        NaN        NaN         NaN         NaN
              AIR  AIR_MA10D  AIR_MA30D  AIR_MA50D  AIR_MA100D  AIR_MA200D
DATE                                                                      
2000-01-03  17.56        NaN        NaN        NaN         NaN         NaN
2000-01-04  17.63        NaN        NaN        NaN         NaN         NaN
2000-01-05  17.81        NaN        NaN        NaN         NaN         NaN
2000-01-06  17.94        NaN        NaN        NaN         NaN         NaN