Process Stocks in Files
For this assignment, for each question output your answers in the format:
where xxx and yyy (and so on) are the answers to each question.
# Q1 Stocks of how many companies are given in this data?
# Q2 How many of these companies start with the letter “A”?
# Q3 How many entries in the table correspond to companies starting with the letter “A”?
# Q4 What is the mean opening value for stocks of companies starting with the letter “A”? Round to two decimal digits
# Q5 What is the minimum opening value for stock with the name “AAL”? On which date did this value occur (return the date as given in the table)?
# Q6 What is the total volume of stocks traded of company “YUM”?
# Q7 What is the minimum volume of stocks of “FB” traded and on which day did this occur?
# Q8 Which company’s stock had the highest traded volume on a single day?
import pandas as pd
stocks = pd.read_csv('all_stocks_5yr.csv')
print('1.', len(pd.unique(stocks.Name))) # Preprocessing A_stocks = stocks[stocks.Name.str.startswith('A')]
aal_stocks = stocks[stocks.Name == 'AAL'] print('5.', aal_stocks[aal_stocks.open == aal_stocks.open.min()].date.values)
yum_stocks = stocks[stocks.Name == 'YUM'] print('6.', yum_stocks.volume.sum())
fb_stocks = stocks[stocks.Name == 'FB'] print('7.', fb_stocks[fb_stocks.volume == fb_stocks.volume.min()].date.values)
print('8.', stocks[stocks.volume == stocks.volume.max()].Name.values)