Page 333 - Informatics_Practices_Fliipbook_Class12
P. 333
8. Consider two Pandas Series representing the population of two different states, State A and State B, over five
years.
stateAPopulation = pd.Series([900000, 950000, 1000000, 980000, 1020000])
stateBPopulation = pd.Series([800000, 850000, 900000, 920000, 950000])
Write a Python code snippet to perform the following tasks:
(i) Find the year with the highest and lowest population for State A.
(ii) Determine the year(s) with the highest population for State B.
(iii) Calculate the average population for both State A and State B over the five years.
(iv) Find the number of years in which the population of State B was higher than State A.
(v) Identify the years in which the population of State A was below 1 million.
(vi) Calculate the average population for State B.
9. Consider two Pandas Series, temperatureA and temperatureB, representing daily temperature
measurements for two different cities, City A and City B, over a period of time. Write a Python code snippet to
perform the following tasks:
(i) Add the two Series to calculate the average temperature for each day.
(ii) Find the temperature difference between the two cities.
(iii) Compute the temperature ratio between the two cities.
10. Consider the following purchase data for a week stored in the form of a dictionary comprising PurchaseID,
Date, Product, Quantity, and Price:
purchaseData = {
'PurchaseID': [101, 102, 103, 104, 105],
'Date': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'],
'Product': ['Laptop', 'Headphones', 'Tablet', 'Mouse', 'Keyboard'],
'Quantity': [1, 2, 1, 3, 2],
'Price': [1000, 150, 400, 15, 30]
}
(i) Create a DataFrame purchaseDF from the provided dictionary.
(ii) Retrieve and display the data for the third purchase (PurchaseID 103) using .loc.
(iii) Retrieve and display the data for the fifth purchase (PurchaseID 105) using .iloc.
(iv) Retrieve and display the Date and Quantity data for the first two purchases using .loc.
(v) Retrieve and display the Product and Price data for the last three purchases using .iloc.
11. Consider the DataFrame purchaseDF mentioned in the previous question and write the code snippet for the
following queries:
(i) Retrieve the columns PurchaseID and Product.
(ii) Retrieve rows with Quantity greater than 2.
(iii) Retrieve rows where the Product is Laptop.
(iv) Use the iloc function to select the rows from index 1 to index 3.
(v) Set row labels to ['A', 'B', 'C', 'D', 'E']. What is the updated DataFrame?
(vi) Retrieve the summary statistics of the DataFrame.
(vii) Determine the minimum price.
Practical 319

