Search code examples
pythonpandasdataframecsvyfinance

Read .csv column and write result/value next to corresponding string?


I have a list of 100 tickers ("ticker" column) in a .csv file. I have a blank/empty "Net Tangible Assets" column next to the "ticker" column. How do I lookup the ticker column, and write the "Net Tangible Assets" total next to it? This is what I have so far, but it only does one ticker.

import yfinance as yf

appl = yf.Ticker("appl")

appl.balance_sheet

df = appl.balance_sheet
df.loc['Net Tangible Assets'][0]

Result:

65339000000.0
import csv
mylist = [[], []]
with open('file.csv', 'r') as f:
    reader = csv.reader(f)
    mylist = list(reader)
    
mylist[1][1] = 65339000000.0
with open("out.csv", 'w', newline="") as out:
    csv_writer = csv.writer(out)
    csv_writer.writerows(mylist)

I would like the output to be like this:

Ticker,Tangible_BV
AAPL,65339000000.0
MSFT,84477000.0
AMD,5319000.0

Solution

  • I would recommend using pandas instead of the csv library.

    import yfinance as yf
    import pandas as pd
    
    # Create a function that returns the latest asset
    def get_latest_asset(stock_id):
        stock = yf.Ticker(stock_id)
        df = appl.balance_sheet
        assets = df.loc['Net Tangible Assets']
        latest_asset = assets[0]
        
        return latest_asset
    
    # Now use a simple for loop to iterate through the id's you choose
    stock_ids = ['AAPL', 'MSFT', 'AMD']
    
    latest_assets = [] # append all the assets to this list
    for id in stock_ids:
        latest_assets.append(get_latest_asset(id))
    
    # Create a dataframe from the data we collected
    assets_df = pd.DataFrame({'Ticker': stock_ids, 'Tangible_BV': latest_assets})
    assets_df.to_csv('assets.csv')
    

    This code has been purposely made "long" for ease of understanding. It is not the most 'elegant' way. If you are looking for something "shorter", you may use the following snippet (has the same functionality):

      import yfinance as yf
      import pandas as pd
    
      stock_ids = ['AAPL', 'MSFT', 'AMD']
      assets = [yf.Ticker(x).balance_sheet.loc['Net Tangible Assets'][0] for x in stock_ids ]
    
      assets_df = pd.DataFrame({'Ticker': stock_ids, 'Tangible_BV': assets})
      assets_df.to_csv('assets.csv')