Check if Dataframe is empty and print results

Tags: , ,



I would like to go over an excel file with different stock symbols. How can I check after reading the stocks values (Open,Close,High,Low,Volume) in a dataframe with yahoo, if the dataframe is empty? In this excel list are more than 700 Symbols and some times yahoo have no data for some symbols. So I would like to exclude this symbols, when I go to the loop.

I am struggling with the following code: if df.empty = True: print (stock).

yf.pdr_override()
start = dt.datetime(2020,6,1)
now = dt.datetime.now()

stocklist = pd.read_csv('symbols/Input_ETF_alle_test.csv') 

# stocklist = stocklist.head()
for i in stocklist.index:
 stock = str(stocklist['Symbol'][i]) 
 print (stock)

 df = yf.download(stock,start,now)
 if df.empty = True:
     print (stock)

Answer

You need to change the if to:

if df.empty == True:

or

if df.empty:


Source: stackoverflow