[Python] Create row for each data in list - python

Discussão em 'Python' iniciado por Stack, Janeiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I´m trying to create a code to show me some stock stats. For that I need to iterate through a list of stocks in python and for each one show some details.

    So far I have this code:

    import yfinance as yf
    import pandas as pd

    tickerlist = ['AAPL', 'MSFT', 'AMZN']

    stock_data = []
    for stock in tickerlist:
    info = yf.Ticker(stock).info
    stock_data.append(info.get("symbol"))
    stock_data.append(info.get("bookValue")))

    df = pd.DataFrame(stock_data)
    df


    But it shows only one column with all data instead of one line per stock, with data headers. image_of_output_data

    I tweaked the code a little bit and I got one row per item but it repeats the last item only, instead of add each entry to row with its values.

    import yfinance as yf
    import pandas as pd

    tickerlist = ['AAPL', 'MSFT', 'AMZN']

    for stock in tickerlist:
    info = yf.Ticker(stock).info
    df = pd.DataFrame({
    'symbol': info.get("symbol"),
    'bookValue': info.get("bookValue")}, index=('0', '1', '2'))

    df



    image_of_output_data2

    I'm new to python and my knowledge led me to those snippets, if anyone can help.

    Thank you!

    Continue reading...

Compartilhe esta Página