Process Stocks in Files
For this assignment, for each question output your answers in the format:
1. xxx
2. yyy
3. ...
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?
Solution:
import pandas as pd
stocks = pd.read_csv('all_stocks_5yr.csv')
1.
print('1.', len(pd.unique(stocks.Name)))
# Preprocessing
A_stocks = stocks[stocks.Name.str.startswith('A')]
2.
print('2.', len(pd.unique(A_stocks.Name)))
3.
print('3.', len(A_stocks))
4.
print('4.', round(A_stocks['open'].mean(),2))
5.
aal_stocks = stocks[stocks.Name == 'AAL']
print('5.', aal_stocks[aal_stocks.open == aal_stocks.open.min()].date.values[0])
6.
yum_stocks = stocks[stocks.Name == 'YUM']
print('6.', yum_stocks.volume.sum())
7.
fb_stocks = stocks[stocks.Name == 'FB']
print('7.', fb_stocks[fb_stocks.volume == fb_stocks.volume.min()].date.values[0])
8.
print('8.', stocks[stocks.volume == stocks.volume.max()].Name.values[0])