Page 34 - Informatics_Practices_Fliipbook_Class12
P. 34
Day5 51.75
dtype: float64
Write the code snippet for the following queries based on the above mentioned series:
(i) Find the highest and lowest stock prices for Company A.
(ii) Determine the days(s) with the highest stock price for Company A.
(iii) Determine the average stock prices for both companies.
(iv) Find the number of days on which the stock price of Company B was higher than Company A.
(v) Find the days on which the stock price of Company A was below 100.
(vi) Find the average stock price of Company B.
Ans. import pandas as pd
companyA_prices = pd.Series([100.50, 102.75, 105.25, 99.80, 101.20],
index=['Day1', 'Day2', 'Day3', 'Day4', 'Day5'])
companyB_prices = pd.Series([50.25, 49.80, 52.10, 50.90, 51.75],
index=['Day1', 'Day2', 'Day3', 'Day4', 'Day5'])
# i.
companyA_max_price = companyA_prices.max()
companyA_min_price = companyA_prices.min()
# ii.
companyA_max_price_dates = companyA_prices[companyA_prices == companyA_max_price].index
# iii.
average_prices = pd.Series([companyA_prices.mean(), companyB_prices.mean()], index =
['Company A', 'Company B'])
# iv.
days_with_higher_prices = (companyB_prices > companyA_prices).sum()
# v.
dates_below_100 = companyA_prices[companyA_prices < 100].index
# vi.
companyB_average_price = companyB_prices.mean()
3. Consider the following Pandas Series:
series= pd.Series([10, 20, 30, 40, 50], index=['a', 'b', 'c', 'd', 'e']).
Determine the result on execution of following Python statement:
data.loc[['a', 'c', 'e']]
Ans. a 10
c 30
e 50
dtype: int64
4. Consider the following Pandas Series:
series = pd.Series([100, 200, 300, 400, 500])
Determine the output on execution of the following Python statement:
series.head(3).sum()
Ans. 600
5. Consider the following Pandas Series:
series= pd.Series([True, False, True, True, False])
Determine the output on execution of the following Python statement:
series.sum()
Ans. 3
20 Touchpad Informatics Practices-XII

