删除 yfinance 数据框的股票行

问题描述 投票:0回答:1

我正在尝试从金融创建一个数据框和一个 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)

所有数据

数据框的第一行

谢谢您的帮助

python jupyter-notebook yfinance
1个回答
0
投票

添加一行

data.columns=["Ticker", "Date", "Open", "Close", "High", "Low", "Volume"]

此行之后

data = data[["Ticker", "Date", "Open", "Close", "High", "Low", "Volume"]]

= = =

复制您的代码并添加新行,

import yfinance as yf
import pandas as pd
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)
    #print(data)
    #x1=input("something1")
    # Reset the index to have Date as a column
    data.reset_index(inplace=True)
    #print(data)
    #x1=input("something2")

    # Add the Ticker column
    data['Ticker'] = stock
    #print(data)
    #x1=input("something3")

    # Remove the 0's row

    # Keep only necessary columns
    data = data[["Ticker", "Date", "Open", "Close", "High", "Low", "Volume"]]
    data.columns=["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)
© www.soinside.com 2019 - 2024. All rights reserved.