Ip 065 PT 4
Ip 065 PT 4
PERIODIC TEST IV
INFORMATICS PRACTICES (IP065)
1. What is the correct syntax to return both the first row and the second row in a Pandas DataFrame df?
A) df.loc[[0,1]] B) df.[[0,1]] C) df.loc[[0-1]] D) df.[[0-1]]
2. If df is a dataframe then print(df) will print
A) The first ten rows B) The first five rows
C) The first five and last five rows D) The first ten and the last ten rows
3. Pandas Series can be created from:
A) Scalar values B) NumPy arrays C) Dictionary D). All of the above
4. Which function is used to find values from a DataFrame D using the index number?
A) D.loc B) D.iloc C) D.index D) None of these
5. Which attribute of a dataframe is used to convert a row into columns and columns into rows in a dataframe?
A) T B) ndim C) empty D) shape
6. The axis 1 identifies a dataframe's
A) row B) columns C) values D) datatype
7. To display the 3rd, 4th and 5th columns from the 6th to 9th rows of a dataframe you can write
A) DF.loc[6:9, 3:5] B) DF.loc[6:10, 3:6] C) DF.iloc[6:10, 3:6] D) DF.iloc[6:9, 3:5]
8. To change the 5th column's value at 3rd row as 35 in dataframe DF, you can write
A) DF[4, 6] = 35 B) DF.iat[4, 6] = 35 C) DF[3, 5] = 35 D) DF.iat[3, 5] = 35
9. Assertion (A): Data visualization refers to the graphical representation of information and data using visual
elements like charts, graphs and maps etc.
Reason (R) : To install matplotlib library we can use the command pip install matplotlib.
A) Both A and R are true and R is the correct explanation of A
B) Both A and R are true but R is not the correct explanation of A
C) A is true but R is false
D) A is false but R is true
10. Write a program to create a series from ndarray with elements ‘a’,’b’,’c’,’d’
11. Write python code to create the following series using Dictionary:
101 Harsh
102 Arun
103 Ankur
104 Harpahul
105 Divya
106 Jeet
12. Write python code to write DataFrame data into “a.csv” file.
SECTION C (20 MINS)(2*3 = 6 MARKS)
A) Choose the correct function to rename city columns to location using rename() function:
a. df.rename(columns={‘City’:’Location’}) b. df.rename(columns={‘City’=’Location’})
c. df.rename(‘City’=’Location’) d. df.rename(df.columns(‘City’,’Location’))
B) df.index properties can be used to
a. rename rows b. rename columns
c. rename rows and columns both d. None of these
C)To display 2 rows from the top in the dataframe, which of the following statement is correct?
a. df.head()=2 b. df.head(n=2) c. df.head(range(2)) d. All of the above
15. Mr.Sharma is trying to write a code to plot line graph shown in fig-1. Help Mr. Sharma to fill in the
blanks of the code and get the desired output.
1. What is the correct syntax to return both the first row and the second row in a Pandas DataFrame df?
A. df.loc[[0,1]] B. df.[[0,1]] C. df.loc[[0-1]] D. df.[[0-1]]
2. If df is a dataframe then print(df) will print
A. The first ten rows B. The first five rows
C. The first five and last five rows D. The first ten and the last ten rows
3. Pandas Series can be created from:
A. Scalar values B. NumPy arrays C. dictionary D. All of the above
4.Which function is used to find values from a DataFrame D using the index number?
a) D.loc b) D.iloc c) D.index d) None of these
5.Which attribute of a dataframe is used to convert rowa into columns and columns into rows in a dataframe?
a) T b) ndim c) empty d) shape
6.The axis 1 identifies a dataframe's
(a) row (b) columns (c) values (d) datatype
7.To display the 3rd, 4th and 5th columns from the 6th to 9th rows of a dataframe you can write
(a) DF.loc[6:9, 3:5] (b) DF.loc[6:10, 3:6] (c) DF.iloc[6:10, 3:6] (d) DF.iloc[6:9, 3:5]
8. To change the 5th column's value at 3rd row as 35 in dataframe DF, you can write
(a) DF[4, 6] = 35 (b) DF.iat[4, 6] = 35 (c) DF[3, 5] = 35 (d) DF.iat[3, 5] = 35
9. Assertion (A) : Data visualization refers to the graphical representation of information and data using visual
elements like charts, graphs and maps etc.
Reason (R) : To install matplotlib library we can use the command pip install matplotlib.
A. Both A and R are true and R is the correct explanation of A
B. Both A and R are true but R is not the correct explanation of A
C. A is true but R is false
D. A is false but R is true
10. Write a program to create a series from ndarray with elements ‘a’,’b’,’c’,’d’
Ans.
import pandas as pd
import numpy as np
data =np.array([‘a’,’b’,’c’,’d’])
s =pd.Series(data)
print(s)
Its output is as follows –
0a
1b
2c
3d
dtype: object
11. Write python code to create the following series using Dictionary:
101 Harsh
102 Arun
103 Ankur
104 Harpahul
105 Divya
106 Jeet
import pandas as pd
D={101:“Harsh”,102:”Arun”,103:”Ankur”,104:”Harpahul”,105:”Divya”,106:”Jeet” }
s=pd.Series(D)
print(s)
106 Jeet
12. Write python code to write DataFrame data into “a.csv” file.
import pandas as pd
Dic={ ‘empno’: (101,102,103,104), ’name’:(‘a’,’b’,’c’,’d’),
’salary’: (3000,5000,8000,9000)}
df=pd.DataFrame(Dic)
df.to_csv(“a.csv”)
Note: csv can be opened in excel, notepad, etc.
13.Consider the following code and answer questions:
Riyaz is creating an application using pandas library in his program , his code is mentioned below. Fill in the
blanks to help him
import _____ as pd #Statement A
d={‘a’:[1,2],’b’:[2,3]}
d2={‘a’:[4,5],’b’:[6,7]}
df1=pd.DataFrame(d)
df2=pd.________(d2) # Statement B
df3=pd._____([df1,df2]) # Statement C
A)Choose the right code from the following for statement A.
a) pandas b) df c) data d) pd
B)Choose the right code from the following for the statement B.
a) Dataframe b) DataFrame c) Series d) Dictionary
C)Choose the right code from the following for the statement C.
a) df.index b) df.shape() c) df.appenddf() d) df.concat()
14.
Consider this Dataframe from all questions given below
A) Choose the correct function to rename city columns to location using rename() function:
a. df.rename(columns={‘City’:’Location’}) b.
df.rename(columns={‘City’=’Location’})
c. df.rename(‘City’=’Location’) d. df.rename(df.columns(‘City’,’Location’))
B) df.index properties can be used to
a. rename rows b. rename columns
c. rename rows and columns both d. None of these
C)To display 2 rows from the top in the dataframe, which of the following statement is
correct:
a. df.head()=2 b. df.head(n=2) c. df.head(range(2)) d. All of the above
SECTION D
15.
i) Which of the above statement is responsible for plotting the values on canvas.
a) Statement 8 b) Statement 4 c) Statement 1 d) None of the above
ii) Statements 5 & 6 are used to give names to x-axis and y-axis as shown in fig.1. Which of the
following can fill those two gaps
a) plt.xlabel('x - axis') plt.ylabel('y - axis') b) plt.xtitle('x - axis') plt.ytitle('y - axis')
c) plt.xlable('x - axis') plt.ylable('x - axis') d) plt.xlabel('x axis') plt.ylabel('y axis')
iii) Raman has executed code with first 7 statements. But No output displayed. which of the
following statements will display the graph?
a) plt.display() b) plt.show() c) matplotlib.pyplot.show() d) Both b & c
iv) The number of markers in the above line chart are
a) zero b) three c) Infinite d) One
16. (i) Write python code to create the following series
101 Harsh
102 Arun
103 Ankur
104 Harpahul
105 Divya
106 Jeet
(ii) Show details of 1st 3 employees using head function
(iii) Show details of last 3 employees using tail function
(iv) Show details of 1st 3 employees without using head function
(v) Show details of last 3 employee without using tail function
(vi) Show value of index no 102
(vii) Show 2nd to 4th records
(viii) Show values of index no=101,103,105
(ix) Show details of “Arun”
Ans.
(i) import pandas as pd
name=[‘Harsh’,’Arun’,’Ankur’,’Harpahul’,’Divya’,’Jeet’]
p=pd.Series(name,index=[101,102,103,104,105,106])
print (p)
(ii) print (p.head(3))
(iii) print (p.tail(3))
(iv) print(p[:3]) or pirnt(p.loc[101:103]) or print(p.iloc[0:3]) or print(p[[101,102,103]])
(v) print (p[-3:]) or print(p[3:]) or print(p[[104,105,106]])
(vi) print(p[102]) or print(p.loc[102])
(vii) print(p[1:4])
(viii) print(p[[101,103,105]])
(ix) print(p[p= =’Arun’])
17. If you are given to plot a histogram using numpy array as per the code given below then answer any of
four question from (i) to (v)
from matplotlib import _____ as plt
import numpy as np
fig, ax = plt.______ (1, 1)
a= np.array([26,59,44,39,76,16,23,11,18,78])
ax.hist(a, bins=[0,10,20,30,40])
ax._____ (‘Histogram’)
ax.set_xticks ([0,10,20,30,40, ])
ax.set_xlabel(‘Percentage’)
ax._______ (‘Students’)
______
(i) Choose the correct option to import for given program:
(a) matplotlib (b) matplot (c) pyplot (d) plot
(ii) Fill in the blank at Line 3
(a) subplots (b) subplot (c) plot (d) subplt
(iii) Which statement is used to set title in Line 6?
(a) title (b) set_title (c) set (d) Title
(iv) How to set Y-axis label?
(a) label () (b) set_y (c) set_ylab () (d) set_ylabel
(v) To fill in blank on Line 10 for showing histogram what can bee used?
(a) plt.show() (b) plt_show() (c) plot_show() (d) plt.show