Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write "null" if column doesn't exist with KeyError: "['Column'] not in index" in df.to_csv?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading