[Python] Drop the ticker row of yfinance dataframe

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a dataframe and a CSV of stocks from finance but I keep getting the NA,NA,AAPL... as my first row how can I fix it?

    my 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)


    all_data

    The first row of the dataframe

    Thank you for your help

    Continue reading...

Compartilhe esta Página