pandaslistyfinance

How to convert yfinance pandas to python list?


I am trying to download financial data using yfinance which seems to return a panda, and try to convert that to a simple python list. Here is the complete code:

import yfinance as yf

# Download 1-minute intraday data for Airbus 
df = yf.download("AIR.DE", interval="1m", period="1d")

# Get datetime list and close prices
x = df.index.tolist()          
y = df["Close"].tolist()       

However I get an error

AttributeError: 'DataFrame' object has no attribute 'tolist'

So how to convert the float values of the panda to a list of floats?


Solution

  • You can use multi-level column access to resolve this issue.

    Here is the example:

    import yfinance as yf
    
    # Download 1-minute intraday data for Airbus 
    df = yf.download("AIR.DE", interval="1m", period="1d")
    # For multi-level columns, you might need:
    x = df.index.tolist()  # This should work for the index
    y = df[('Close', 'AIR.DE')].tolist()  # Multi-level column access