Home > Enterprise >  Iterate though list loop error help - Yfinance
Iterate though list loop error help - Yfinance

Time:11-13

I created a list of tickers which I'm trying to get certain information from through Yfinance. The problem I'm facing is trying to get certain data such as ebitda, enterprise value, and total assets all in one script. I've looked through plenty of material and tried different methods but I cannot figure it out.

Have a great day. Thanks!

The program that works for one request

import pandas as pd
import numpy as np
import yfinance as yf

tickers = ['AAPL', 'GOOGL', 'FB']
a = [yf.Ticker(t).info.get('ebitda', 'NaN') for t in tickers]

pd.set_option("display.max_rows", None)
df = pd.DataFrame(a, tickers, columns=['Ebitda'])
print(df)

Output

             Ebitda
AAPL   120233000960
GOOGL   85196996608
FB      54758998016

Here is what I'm trying to do that I need help on. this script results in an init() TypeError for the df variable (for pandas data frame)

import pandas as pd
import numpy as np
import yfinance as yf

tickers = ['AAPL', 'GOOGL', 'FB']

for t in tickers:
    a = yf.Ticker(t).info.get('ebitda', 'NaN')
    b = yf.Ticker(t).info.get('enterpriseValue', 'NaN')
    c = yf.Ticker(t).info.get('totalAssets', 'NaN')


pd.set_option("display.max_rows", None)  
df = pd.DataFrame(a, tickers, b, c, columns=['Ebitda', 'EnterpriseValue', 'TotalAssets',])
print(df)

Desired Output

Ticker       Ebitda    Enterprise Value    TotalAssets
AAPL   120233000960    2460000000000       323890000000
GOOGL   85196996608    1980000000000       275909000000
FB      54758998016     833000000000       133376000000

CodePudding user response:

If you use for-loop then using normal assigment a = ... you can get only last value.

It is standard rule: if you use for-loop then you have to use list to get all results.

BTW: You could run Ticker(t) only once in loop - maybe it will run faster.

import pandas as pd
import yfinance as yf

tickers = ['AAPL', 'GOOGL', 'FB']

column_a = []  # <-- list for all results
column_b = []  # <-- list for all results
column_c = []  # <-- list for all results

for t in tickers:
    print('---', t, '---')
    
    all_info = yf.Ticker(t).info

    a = all_info.get('ebitda', 'NaN')
    b = all_info.get('enterpriseValue', 'NaN')
    c = all_info.get('totalAssets', 'NaN')
    
    column_a.append(a)  # <-- list for all results
    column_b.append(b)  # <-- list for all results
    column_c.append(c)  # <-- list for all results

#pd.set_option("display.max_rows", None)

df = pd.DataFrame({
    'Tickets': tickers,
    'Ebitda': column_a,           # <-- list for all results
    'EnterpriseValue' :column_b,  # <-- list for all results
    'TotalAssets': column_c,      # <-- list for all results
})
print(df)

EDIT:

Eventually you could append directly to DataFrame

First you have to create empty DataFrame but with all columns.

Next you can append dictionary with ignore_index=True. And in DataFrame you have to assign it again to DataFrame - df = df.append(...)

import pandas as pd
import yfinance as yf

tickers = ['AAPL', 'GOOGL', 'FB']

df = pd.DataFrame(columns=['Tickets', 'Ebitda', 'EnterpriseValue', 'TotalAssets'])

for t in tickers:
    print('---', t, '---')
    
    all_info = yf.Ticker(t).info
    
    a = all_info.get('ebitda', 'NaN')
    b = all_info.get('enterpriseValue', 'NaN')
    c = all_info.get('totalAssets', 'NaN')
  
    df = df.append({
        'Tickets': t,
        'Ebitda': a,
        'EnterpriseValue': b,
        'TotalAssets': c,
    }, ignore_index=True)  

#pd.set_option("display.max_rows", None)
    
print(df)
  • Related