How to write dataframe to csv for max date rows only (filter for max date rows)?

Question:

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)
Asked By: HTMLHelpMe

||

Answers:

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)
Answered By: SR3142
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.