Process Stocks in Files
To complete the python assignment , for each question output your answers in the format:
- xxx
- yyy
- ...
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')
print('1.', len(pd.unique(stocks.Name))) # Preprocessing A_stocks = stocks[stocks.Name.str.startswith('A')]
print('2.', len(pd.unique(A_stocks.Name)))
print('3.', len(A_stocks))
print('4.', round(A_stocks['open'].mean(),2))
aal_stocks = stocks[stocks.Name == 'AAL'] print('5.', aal_stocks[aal_stocks.open == aal_stocks.open.min()].date.values[0])
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[0])
print('8.', stocks[stocks.volume == stocks.volume.max()].Name.values[0])
Similar Samples
Explore our diverse range of programming samples to see the quality and expertise we bring to every project. Each sample showcases our ability to tackle complex problems and deliver clear, efficient solutions. Whether you're a beginner or advanced programmer, our samples provide valuable insights and inspiration for your own work.
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python