Using Python 3.6
I have a Pandas dataframe converted from a panel using the following code:
stocks = ['AAPL', 'JPM', 'JNJ', 'PG', 'XOM', 'VZ', 'IEF']
pnl = web.get_data_yahoo(stocks, start, end, interval = 'd')
df = pnl.to_frame().unstack(level=1)
df.columns = df.columns.swaplevel(0,1)
If I then run df in the console it returns in this format (with 'Open', 'Low', 'Adj Close', etc):
minor             IEF         JNJ         JPM          PG          VZ  
               Volume      Volume      Volume      Volume      Volume   
Date                                                                    
2017-11-17   941200.0   4112100.0   8989200.0   5919900.0  18732200.0   
2017-11-16  1749200.0   4193800.0   9808000.0   7205100.0  13818000.0   
2017-11-15  1933100.0   4811500.0  15384100.0   6688100.0  15726500.0 
Now, the problem I am running into arises when I try and calculate a simple moving average, for let's say AAPL, using: df['30sma_AAPL'] = df['AAPL']['Adj Close'].rolling(window=30, min_periods=0).mean().
This command adds the new column, but doesn't put the values within the df['AAPL'] group (I don't know if group is the correct word?). For example, if I run df['AAPL'], it outputs the 'Adj Close', 'Close', 'High', etc. for AAPL specifically. Basically, I want to have a loop that calculates the sma for each stock and puts the values into their group. Is this possible with this dataframe?
EDIT: My desired output (fake #'s):
minor             IEF         JNJ         JPM          PG          VZ  
               Volume      Volume      Volume      Volume      Volume   
Date                                                                    
2017-11-17   941200.0   4112100.0   8989200.0   5919900.0  18732200.0   
2017-11-16  1749200.0   4193800.0   9808000.0   7205100.0  13818000.0   
2017-11-15  1933100.0   4811500.0  15384100.0   6688100.0  15726500.0 
minor             IEF         JNJ         JPM          PG          VZ  
               30_sma      30_sma      30_sma      30_sma      30_sma   
Date                                                                    
2017-11-17      100.0          50          25          26          45   
2017-11-16         25          40         100          89         100   
2017-11-15         75          30          12          75          60 
 
    