'How to add a DataFrame to MultiIndex DataFrame columns?
import pandas as pd
import yfinance as yf
import numpy as np
tickers = ['BRIGADE.NS', 'DLF.NS', 'GODREJPROP.NS', 'OBEROIRLTY.NS', 'PRESTIGE.NS']
Tickers_Data = yf.download(tickers, period='5y')
display(Tickers_Data)
Ret_Log = np.log(Tickers_Data['Adj Close'] / Tickers_Data['Adj Close'].shift(1))
Ret_Cumulative = Ret_Log.cumsum().apply(np.exp)
Ret_Absolute = Tickers_Data['Adj Close'].pct_change()
MA50 = Tickers_Data['Open'].rolling(50).mean()
MA200 = Tickers_Data['Open'].rolling(200).mean()
display(Tickers_Data.columns)
I want to add Ret_Log, Ret_Cumulative, Ret_Absolute, MA50, MA200 at the end of my MultiIndex DataFrame Tickers_Data. If I run for loop (added at the end of code)
I get the expected output but I need to achieve this without a for loop
.
Any help is highly appreciated. Thanks in advance.
If I use the code below, I get the expected output but I need to achieve this without a for loop
for i in range(len(tickers)):
tickers_data['MA50',tickers[i]] = tickers_data['Open'][tickers.index[i]].rolling(50).mean()
for i in range(len(tickers)):
tickers_data['MA200',tickers[i]] = tickers_data['Open'][tickers.index[i]].rolling(200).mean()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|