I am getting KeyError: "['CashFinancial'] not in index" on the df.to_csv line because ‘GOOG’ doesn’t have the CashFinancial column. How can I have it write in null for the CashFinancial value for ‘GOOG’?
import pandas as pd
from yahooquery import Ticker
symbols = ['AAPL','GOOG','MSFT'] #This will be 75,000 symbols.
header = ["asOfDate","CashAndCashEquivalents","CashFinancial","CurrentAssets","TangibleBookValue","CurrentLiabilities","TotalLiabilitiesNetMinorityInterest"]
for tick in symbols:
faang = Ticker(tick)
faang.balance_sheet(frequency='q')
df = faang.balance_sheet(frequency='q')
df.to_csv('output.csv', mode='a', index=True, header=False, columns=header)
>Solution :
What about :
if tick == "GOOG"
df.loc[:,"CashFinancial"] = None
To set an entire CashFinancial column to "None" only if your "tick" was GOOG, before writing it to csv.
The full code from the example you posted would he something like :
import pandas as pd
from yahooquery import Ticker
symbols = ['AAPL','GOOG','MSFT']
header = ["asOfDate","CashAndCashEquivalents","CashFinancial","CurrentAssets","TangibleBookValue","CurrentLiabilities","TotalLiabilitiesNetMinorityInterest"]
for tick in symbols:
faang = Ticker(tick)
faang.balance_sheet(frequency='q')
df = faang.balance_sheet(frequency='q')#,{"symbol":[1],"asOfDate":[2],"CashAndCashEquivalents":[3],"CashFinancial":[4],"CurrentAssets":[5],"TangibleBookValue":[6],"CurrentLiabilities":[7],"TotalLiabilitiesNetMinorityInterest":[8],"marketCap":[9]}
for column_name in header :
if not column_name in df.columns :
#Here, if any column is missing from the names you defined
#in your "header" variable, we add this column and set all
#it's row values to None
df.loc[:,column_name ] = None
df.to_csv('output.csv', mode='a', index=True, header=False, columns=header)