Script_Pivot:
import pandas as pd
# Load the data from the Excel file
input_file_path = 'BSE500_StockPrices.xlsx' # Adjust the path if necessary
all_data = pd.read_excel(input_file_path)
# Pivot the DataFrame to have dates as the index and stocks as columns
pivoted_data = all_data.pivot(index='Date', columns='Ticker', values='Adj Close')
# Resetting the index to make it a regular DataFrame
pivoted_data.reset_index(inplace=True)
# Save the pivoted data to a new Excel file
output_file_path = 'BSE500_StockPrices_Pivoted22.xlsx'
pivoted_data.to_excel(output_file_path, index=False)
print(f"Pivoted data saved to {output_file_path}")
Script_Extraction:
import yfinance as yf
import pandas as pd
# Path to your CSV file containing tickers
csv_file_path = 'C:\Users\Cookies\Desktop\bse_500_tickers.csv'
# Read the tickers from CSV
tickers_df = pd.read_csv(csv_file_path)
tickers_list = tickers_df['Tickers'].tolist()
# Create an empty DataFrame to store the data
all_data = [Link]()
# Loop through each ticker and download historical data
for ticker in tickers_list:
print(f"Fetching data for {ticker}...")
# Download weekly data for the past 10 weeks
stock_data = [Link](ticker, period="1y", interval="1wk")
# Check if we successfully fetched the data
if not stock_data.empty:
stock_data['Ticker'] = ticker # Add a column for the stock ticker
# Keep only the Adjusted Close column and Ticker
stock_data = stock_data[['Adj Close', 'Ticker']]
# Append the data to the main DataFrame
all_data = [Link]([all_data, stock_data])
# Reset the index to make it easier to work with
all_data.reset_index(inplace=True)
# Save the final merged data to an Excel file
output_file = 'BSE500_StockPrices.xlsx'
all_data.to_excel(output_file, index=False)
print(f"Data saved to {output_file}")