@francisco
To iterate through pandas-datareader and create multiple dataframes for each stock ticker, you can follow these steps:
1 2 |
import pandas as pd import pandas_datareader.data as web |
1
|
tickers = ['AAPL', 'GOOGL', 'MSFT'] # Update with your desired stock tickers |
1
|
stock_dataframes = {} |
1 2 3 |
for ticker in tickers: data = web.DataReader(ticker, 'yahoo', start_date, end_date) # Update start_date and end_date with your desired date range stock_dataframes[ticker] = data |
Note: Replace 'yahoo' in web.DataReader()
with the appropriate data source if you intend to use a different one.
1
|
apple_dataframe = stock_dataframes['AAPL'] |
Here's the complete example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import pandas as pd import pandas_datareader.data as web tickers = ['AAPL', 'GOOGL', 'MSFT'] stock_dataframes = {} start_date = '2021-01-01' end_date = '2021-12-31' for ticker in tickers: data = web.DataReader(ticker, 'yahoo', start_date, end_date) stock_dataframes[ticker] = data # Accessing the data frames apple_dataframe = stock_dataframes['AAPL'] google_dataframe = stock_dataframes['GOOGL'] microsoft_dataframe = stock_dataframes['MSFT'] |
In this example, each stock ticker is used as the key in the stock_dataframes
dictionary, and the corresponding dataframe is assigned as the value.
@francisco
Here is an example that demonstrates how to iterate through pandas-datareader to create multiple dataframes for each stock ticker:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import pandas as pd import pandas_datareader.data as web # Create a list of stock tickers tickers = ['AAPL', 'GOOGL', 'MSFT'] # Create an empty dictionary to store the dataframes stock_dataframes = {} # Define the date range start_date = '2021-01-01' end_date = '2021-12-31' # Iterate through the tickers and retrieve data using pandas_datareader for ticker in tickers: data = web.DataReader(ticker, 'yahoo', start=start_date, end=end_date) stock_dataframes[ticker] = data # Accessing the dataframes for ticker, dataframe in stock_dataframes.items(): print(f"Stock Ticker: {ticker}") print(dataframe.head()) # Display the first few rows of each dataframe print(" ") |
In this script:
You can modify this example as needed based on your specific requirements and further analyze or manipulate the data for each stock ticker.