0% found this document useful (0 votes)
25 views4 pages

Numpy QP

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

MONTHLY TEST - 2 (JULY 2019)

CLASS : XII COMMERCE MARKS : 70

SUBJECT : INFORMATICS PRACTICES TIME : 03 HOURS


Note: All questions are compulsory. Indentation should be take care of while writing programs.

Section A : Python Revision Tour (20 Marks)

1 Predict the output of the following code snippet: 1


a=[1,2,3,4,5]
print(a[3 : 0 : -1])

2 Find the errors and State the reason : 1


for Name in [Amar, Shweta, Parag]
IF Name[0] = 'S' :
print(Name)

3 Why is dictionary termed as an unordered collection of objects ? 1

4 Suppose L=["abc",[6,7,8],3,"mouse"], consider the list and answer the following: 2


a) L[3 : ]
b) L[ : : 2]
c) L[1 : 2]
d) L[1][1]

5 Find the output of the following : 2


word="green vegetables"
print(word.find('g',2))
print(word.find('veg',2))
print(word.find('tab',4,15))

6 Find errors in the following code (if any) and correct the code by rewriting it and underlining the 2
corrections :
x = int ("Enter value for x : "))
for in range [0,11]:
if x = y
print x+y
else:
Print x-y

7 Write a Python program to combine two dictionaries by adding values for common keys : 2
d1={'a':100, 'b':200, 'c':300}
d2={'a':300, 'b':300, 'd':600}
The output dictionary should look like this sample :
NewDict={'a':400, 'b':500, 'c':300, 'd':600}

8 Find the output of the following : 3


Text = "gmail@com"
L = len(Text)
nText = ""
for i in range (0,L):
if Text[i].isupper():
nText = nText + Text[i].lower()
elif Text[i].isalpha():
nText = nText + Text[i].upper()
else:
nText = nText + '#&'
print(nText)
9 What will be the output of the following code snippet ? 3
mydict = {}
mydict[(1,2,4)] = 8
mydict[(4,2,1)] = 10
mydict[(1,2)] = 12
sum = 0
for k in mydict :
sum += mydict[k]
print(sum)
print(mydict)

10 Create a dictionary whose keys are month names and whose values are the number of days in the 3
corresponding months.
a) Ask the user to enter a month name and use the dictionary to tell them how many days are in the
asked month
b) Print out all the months with 31 days

Section B : NumPy Arrays (20 Marks)

11 What are NumPy arrays ? 1

12 Create an ndarray with values ranging from 10 to 49 each spaced with a difference of 3. 1

13 What functions can you use for joining two or more ndarrays ? 1

14 Predict the output of the following code fragment : 2


import numpy as np
x = [15,22,3,26,8,34,23,51]
x1,x2,x3 = np.split(x, [3,5])
print(x1,x2,x3)

15 Consider the following ndarray Ary1 : 2


array([[1,2,3],
[4,5,6],
[7,8,9]])
Write code to extract the subset from the ndarray Ary1, containing elements whose square is fully
divisible by 4.

16 Consider the ndarray Ary1 given in Question 15, what will be the output produced by the following array 2
slices ?
a) Ary1[ : 3, ::2]
b) Ary1[::-1, ::-1]

17 Define and write syntax for Covariance. 2

18 Write the process and syntax of joining NumPy arrays along with an example for each of : 3
hstack( ), vstack( ) and concatenate( )

19 Consider the following ndarray : 3


ary=array([[0,1,2,3,4,5], twos=array([[2,2,2,2,2,2],
[6,7,8,9,10,11], [2,2,2,2,2,2],
[12,13,14,15,16,17], [2,2,2,2,2,2],
[18,19,20,21,22,23]]) [2,2,2,2,2,2]])

Write the output of the following:


a) ary - 6
b) np.divide(ary , 2)
c) ary * twos

20 Consider the following array A : 3


array([[0,1,2,3,4,5],
[6,7,8,9,10,11],
[12,13,14,15,16,17]])
a) Write commands to split the following array vertically into three equal parts
b) Write commands to extract horizontal rows separately
c) To change the order of array into 9 rows

Section C : Pandas - Basic Operations on Dataframes (15 Marks)

21 How to install pandas ? Write procedure in brief. 1

22 Write down the syntax to select/access a subset from a DataFrame using Row/Column name. 1

23 Write down the syntax of iterating over a DataFrame. 1

24 What will be the output produced by the following code : 2


import pandas as pd
Stationery = ['Pencils', 'notebooks', 'scale', 'erasers']
S=pd.Series([20,33,52,10], index=Stationery)
S2=pd.Series([17,13,31,32], index=Stationery)
print(S+S2)

25 Write code statements to address the following, from a DataFrame namely sales : 2
a) Print only columns ‘Item’ and ‘Revenue’
b) Print the value of cell in 5th row, ‘Item’ column

26 Write the code to create an ndarray having 6 zeros in it. Write statement to change 3rd and 5th element 2
of this ndarray to 15 and 25 respectively.

27 Write a Pandas program to create and display a DataFrame from specified dictionary data which has the 3
index labels.
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'], 'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19], 'attempts': [1, 3, 2, 3, 2, 3, 1,
1, 2, 1], 'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Also count and print the number of rows and columns in the program.

28 In the given DataFrame exam_data as mentioned in Question 27, Write down the code snippet to do the 3
following :
a) To insert a new column in the given DataFrame
b) To change the order of given DataFrame Columns
c) To select a row of the given DataFrame by providing an integer index

Section D : Pandas - Advanced Operations on Dataframes (15 Marks)

29 What does quantile function do ? 1

30 What is pivoting ? How it is useful ? 1

31 Write down the syntax of var ( ) function. 1

32 Consider DataFrame wdf as shown below : 2


MinTemp MaxTemp Rainfall Evaporation

0 2.9 8.0 24.3 0.0


1 3.1 14.0 26.9 3.6
2 6.2 13.7 23.4 3.6
3 5.3 13.3 15.5 39.8
4 6.3 17.6 16.1 2.8
5 5.4 18.2 16.9 0.0
6 5.5 21.1 18.2 0.2
7 4.8 18.3 17.0 0.0
8 3.6 20.8 19.5 0.0
9 7.7 19.4 22.8 16.2
10 9.9 24.1 25.2 0.0
11 11.8 28.5 27.3 0.2
12 13.2 29.1 27.9 0.0
13 16.8 24.1 30.9 0.0
14 19.4 28.1 31.2 0.0
15 21.6 34.4 32.1 0.0
16 20.4 33.8 31.2 0.0
17 18.5 26.7 30.0 1.2
18 18.8 32.4 32.3 0.6
19 17.6 28.6 33.4 0.0
20 19.7 30.3 33.4 0.0

a) Write command to calculate minimum value for each of the columns.


b) Write command to calculate maximum value for each of the rows.

33 Consider the DataFrame wdf as given in Question 32 for the following : 2


a) Write command to calculate variance for column Rainfall
b) Write command to calculate mean for last 10 rows.

34 Consider the DataFrame wdf as given in Question 32 for the following : 2


a) Write command to compute mode for column ‘Evaporation’
b) Write command to compute mean for first 10 rows.

35 Consider the DataFrame wdf as given in Question 32 for the following : 3


a) Reindex DataFrame wdf so that two new columns get added to it while previous data is retained
b) Reindex DataFrame wdf so that two new rows get added to it while previous data is retained.
Fill the new rows with value 10.0

36 Using above defined DataFrame wdf as in Question 32, do the following : 3


a) Compute average rainfall and write command to compute it
b) Write command to compute sum of every column of DataFrame wdf

***

You might also like