How do I get the df.to_csv
to write only rows with the max asOfDate
? So that each symbol
below will only one row?
import pandas as pd
from yahooquery import Ticker
symbols = ['AAPL','GOOG','MSFT'] #There are 75,000 symbols here.
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')
for column_name in header :
if column_name not in df.columns:
df.loc[:,column_name ] = None
#Here, if any column is missing from your header column names for a given "tick", we add this column and set all the valus to None
df.to_csv('output.csv', mode='a', index=True, header=False, columns=header)
CodePudding user response:
if asOfDate column has a date type or of it is a string with date in the format yyyy-mm-dd you can filter the dateframe for the rows you want to write
df[df.asOfDate == df.asOfDate.max()].to_csv('output.csv', mode='a', index=True, header=False, columns=header)