Я пытаюсь создать фрейм данных и CSV-файл с акциями финансового сектора, но в первой строке все равно получаю NA,NA,AAPL... как это исправить?
мой код:
import os
# Define the list of stock symbols and date range
stocks = ["AAPL", "AMZN", "NFLX"]
start_date = "2020-01-01"
end_date = "2023-01-01"
stock_data_path = "***/data"
# Ensure the save path exist
os.makedirs(stock_data_path, exist_ok=True)
# Loop through the stocks and download the data
for stock in stocks:
print(f"Fetching data for {stock}...")
data = yf.download(stock, start=start_date, end=end_date)
# Reset the index to have Date as a column
data.reset_index(inplace=True)
# Add the Ticker column
data['Ticker'] = stock
# Remove the 0's row
# Keep only necessary columns
data = data[["Ticker", "Date", "Open", "Close", "High", "Low", "Volume"]]
# Save the data to a CSV file
file_path = os.path.join(stock_data_path, f"{stock}_stock_data.csv")
data.to_csv(file_path, index=False)
# Combine all files into a single DataFrame and save to CSV
for stock in stocks:
file_path = os.path.join(stock_data_path, f"{stock}_stock_data.csv")
stock_data = pd.read_csv(file_path)
# Concating so the all_data will have only the 7 columns that are shared by all the stocks
if stock == stocks[0]:
all_data = stock_data
else:
all_data = pd.concat([all_data, stock_data], ignore_index=True)
# Save combined data to CSV
all_data_file_path = os.path.join(stock_data_path, "all_stocks_data.csv")
all_data.to_csv(all_data_file_path, index=False)
all_data
Первая строка фрейма данных
Спасибо за помощь
Я пытаюсь создать фрейм данных и CSV-файл с акциями финансового сектора, но в первой строке все равно получаю NA,NA,AAPL... как это исправить? мой код: [code]import os
# Define the list of stock symbols and date range stocks = ["AAPL", "AMZN", "NFLX"] start_date = "2020-01-01" end_date = "2023-01-01" stock_data_path = "***/data"
# Ensure the save path exist os.makedirs(stock_data_path, exist_ok=True)
# Loop through the stocks and download the data for stock in stocks: print(f"Fetching data for {stock}...") data = yf.download(stock, start=start_date, end=end_date)
# Reset the index to have Date as a column data.reset_index(inplace=True)
# Add the Ticker column data['Ticker'] = stock
# Remove the 0's row
# Keep only necessary columns data = data[["Ticker", "Date", "Open", "Close", "High", "Low", "Volume"]]
# Save the data to a CSV file file_path = os.path.join(stock_data_path, f"{stock}_stock_data.csv") data.to_csv(file_path, index=False)
# Combine all files into a single DataFrame and save to CSV for stock in stocks: file_path = os.path.join(stock_data_path, f"{stock}_stock_data.csv") stock_data = pd.read_csv(file_path)
# Concating so the all_data will have only the 7 columns that are shared by all the stocks if stock == stocks[0]: all_data = stock_data else: all_data = pd.concat([all_data, stock_data], ignore_index=True)
# Save combined data to CSV all_data_file_path = os.path.join(stock_data_path, "all_stocks_data.csv") all_data.to_csv(all_data_file_path, index=False) [/code] all_data Первая строка фрейма данных Спасибо за помощь