Write a function named "read_prices" that takes one parameter that is a list of ticker symbols that your company owns in their portfolio. You are to read a CSV file for each of these tickers which contains the price of each stock throughout the year and return these prices in a single dictionary. The returned dictionary will contain ticker symbols as keys and dictionaries as values where the inner dictionaries will have dates as keys (as strings in the format "YYYY-MM-DD") and prices as values as floats. All said this dictionary will contain the price for any stock on any date over the past year

Respuesta :

Answer:

import pandas pd

def read_prices(tickers):

price_dict = {}

# Read ingthe ticker data for all the tickers

for ticker in tickers:

# Read data for one ticker using pandas.read_csv  

# We assume no column names in csv file

ticker_data = pd.read_csv("./" + ticker + ".csv", names=['date', 'price', 'volume'])

# ticker_data is now a panda data frame

# Creating dictionary

# for the ticker

price_dict[ticker] = {}

for i in range(len(ticker_data)):

# Use pandas.iloc  to access data

date = ticker_data.iloc[i]['date']

price = ticker_data.iloc[i]['price']

price_dict[ticker][date] = price

return price_dict  

ACCESS MORE