101 Onwards On Python Pandas and Pyplot
101 Onwards On Python Pandas and Pyplot
Anil has created a dataframe df which he wants to manage according to given questions.
107. He wants to find all the records with salary less than 1500. He should write?
a) df.salary(data<=1500)
b) df[df.salary<=1500]
c) df['salary'<=1500]
d) None of the above
Ans: b)df[df.salary<=1500]
108. He is asked by his friend to display last 3 rows. What code he should write?
a) df.iloc[-3:]
b) df.loc[-3:]
c) df.tail(3)
d) a,c
e) all
Ans: d) a,c
109. He wants to count total elements of given dataframe. Which command he should choose?
a) df.count()
b) df.values
c) len(df)
d) df.size
Ans: d) df.size
110. Select correct command to delete column 'EmpNo'.
a) df.drop('EmpNo')
b) df.drop('EmpNo',axis=0)
c) df.drop('EmpNo',axis=1)
d) None
Ans: c) df.drop('EmpNo',axis=1)
111. His boss asked him to reset the dataframe with value 0. How can he do this?
a. df.loc[:]=0
b. df.at[L]=0
c. df = 0
d. None of above
Ans: (a) df.loc[:]=0
(a)
10 20
0 30 40
1 50 60
(b)
0 1
10 30 50
20 40 0
(c)
0 1
0 10 20
1 30 40
2 50 60
(d)
0 1 2
0 10 30 50
1 20 40 60
Ans: (c)
115. Consider the following dataframe ‘emp’
ename dept
e1 a.k. om
e2 m.s. ee
Ans: (b) 3 do
4 to
123.Select the correct statement to change rows as columns and columns as rows of
dataframe:
(a) df.t()
(b) df.T
(c) df.transpose()
(d) Df.transpose
124. Which of the following is Not True about Series and Dataframe:
(a) Both are size mutable
(b) Both can be derived from pandas
(c) Both can be reshaped into different forms
(d) Both can be created by passing data in form of list dictionaries and ndarray
125. To access individual item from dataframe ‘df’ which of the following is Not correct?
(a) df.loc[2,2]
(b) df.iat[2,2]
(c) df.at[2,2]
(d) df[0,0]
126. Aastha has given the following command to delete a column ‘pub’ from the ‘book’
dataframe .
book.drop(‘pub’)
But she is not getting the desired output , help her to select the correct statement .
a) book.drop([‘pub])
b) book.drop([‘pub’,axis=0)
c) book.drop([‘pub’,axis=1)
d) none
Ans - c) book.drop([‘pub’,axis=1)
129. Your class teacher wants to update the value of column award of class 4 to 6 which
is currently NaN. Help him to select correct statement
a) df.at[‘class4’,’awards’, ]=6
b) df.loc[‘class4’,’awards’, ]=6
c) both are true
d) both are false
Boys girls
Class1 24 18
Class2 20 20
a) print(df.iloc[1,2:1,2])
b) print(df.iloc[0,3:0,3])
c) print(df.iloc[0,2:0,2])
d) print(df.iloc[1,3:1,3])
Ans : c) print(df.iloc[0,2:0,2])
131. We can add a new r0w ‘Class 5‘ at the end of dataFrame with the values for all columns
as
a. df.at[‘class5’]=[21, 20, 6, 12]
b. df[‘class5’]=[21, 20, 6, 12]
c. df.loc[‘class5’]=[21, 20, 6, 12]
d. df.class5=[21, 20, 6, 12]
e. Both a & c
f. All 0f the above
ename dept
e1 a.k om
e2 m.s ee
a. print(Book.Author)
b. print(Book[‘Author’])
c. Both are true
d. (a) true (b) false
a. With iloc, both start index and end index are included in the result
b. With loc, like slices end label is excluded from result.
c. With iat, we can access individual value for a row/column label pair.
d. To access individual data from a dataframe, we can use row index with
‘<dataframe>.column[]’ in a square bracket.
Ans. d. To access individual data from a dataframe, we can use row index with
‘<dataframe>.column[]’ in a square bracket.
(A) For a given data frame df of shape (3,3) when we are assigning the values using the
following code
Df.column1[23,45,12,21]
Python gives error
(R) While assigning values to a column of a data frame, then the sequence containing
values must be equal to the no of rows in the data frame
139. If the shape of the passed values in the index sequence and the length of the dictionary’s
values are not same, then python gives
(a) Index error
(b) Value error
(c) Key error
(d) Runtime error
Ans. (b) Value error
141. To count total number of rows of dataframe ‘df’ which of the following command can be
written.
(a) len(df)
(b) df.len
(c) df.len()
(d) All are correct
Ans: (b) Display 3rd to 7th row and first three columns.
143. Which of the following is used to merge two dataframes?
(a) Join()
(b) Merge()
(c) Extend()
(d) Append()
Ans: (b) 0
0 [5,3,8]
1 [4,2,7,9]
147. To access the value of data using column labels we can use :
a). loc
b). <dataframe object>.<column label>
c). <dataframe object>[column label]
d). b and c
e). All
148. Consider the following dataframe df and answer your questions from 5-10.
Jaipur 155
Raipur 168
Nagpur 227
Kanpur 201
a). df.sum()
b). df.sum(axis=0)
c). df.sum(axis=1)
d). df.sum
149. Write the appropriate command to display names of all the rows.
a). df.shape
b). df.axis
c). df.labels
d). df.index
Which of the following will return exact no.of values in each columns?
a. df.columns
b. df.count()
c. df.size()
d. df.shape()
Ans: b. df.count
a) skipna
b) fillna
c) replacena
d) inplacena
Ans : b) fillna
a) 'at' accesses a single value for a row/column pair by integer position and 'iat' accesses a
row/column pair using labels
b) 'at' accesses a single value for a row/column pair using labels and 'iat' accesses a
row/column pair by integer position
c) 'at' extracts a subset from the dataframe using row/column labels and 'iat' extracts a
subset using row/column integer position
d) 'at' extracts a subset from the dataframe using row/column integer position and 'iat'
extracts a subset using row/column labels
Ans : b) 'at' accesses a single value for a row/column pair using labels and 'iat' accesses a
row/column pair by integer position
159. To display three rows from the bottom of a dataframe, which of the following can be written
(i). df.tail(3)
(ii). df.iloc[-3:]
a) emp.loc[len(df)]=[1,'raghu', 2300]
b) emp.iloc[len(df)]=[1,'raghu', 2300]
c) emp.iloc[-1]=[1,'raghu',2300]
d) emp.loc[-1]=[1,'raghu',2300]
df=pd.DataFrame(np.array([[5,3,8],[4,2,7]])
print(df.shape)
(a) (3,2)
(b) (2,3)
(c) (2,1)
(d) (1,2)
163. In the following questions two statements are given one assertion denoted with (A) and
other reason denoted with (R). Select the correct answer as per given choices.
Answer: (d.) Both are True and (R) is correct explanation of (A)
164. Which of the following is correct syntax for accessing a particular column of dataframe
(a) df.<columnname>
(b) df.[‘columnname’]
(c) df.loc[:,’columnname’]
(d) All are correct
(a) df=pd.dataframe(dict1)
(b) df=pd.Dataframe(dict1)
(c) df=pd.dataFrame(dict1)
(d) df=pd.DataFrame(dict1)
Ans:(d)
Class1. Class2
Rollno. 1. 2
Name. Amar. Sultan
167. Which of the following is not true about count () used in pandas
(i) By default count() method counts values for each column of datafrrame
(ii) df.count and df.count(axis='index') produces same result
168. When a dataframe is created using 2D dictionary,column labels are formed by:
169. To extract the first three rows and three columns of a dataframe 'exp' which of the following
is true:
(a) exp.iloc[0:2,0:2]
(b) exp.iloc[0:3,0:3]
(c) exp.iloc[1:3,1:3]
(d) exp.iloc[1:4,1:4]
Ans:(b) exp.iloc[0:3,0:3]
172. While accessing a subset from a dataframe using loc, the end label is always included in
the result set?
(a)True
(b)False
Ans: (a)True
173. Select appropriate code to list the values of cell in 5th row in ‘item’ column
(a)df.loc[4,’item]
(b)df.loc[5,’item’]
(c)df.at[4,’item’]
(d) a and c
Ans: (d) a and c
176.To access individual item from a dataframe 'df' which of the following is not correct:
a. df.at[row_index, , column_index]
b. df.iat[row_index, column_index]
C. df.<column label>[row_index]
177.In the following questions two statements are given one assertion denoted with (A) and
other reason denoted with (R). Select the correct answer as per given choices.
df = pd.DataFrame(np.array([[5,3,8], [4,2,7,9]]))
print(len(df))
[5,3,8]
[4,2,7,9]
(r) python create only single column in dataframe object if data passed to it is ndarray with rows
of different length.
a. (A) is False but (R) is True b. (A) is True but (R) is False
Ans. (d) Both are True and (R) is correct explanation of (A)
c. Print(class.loc[['class 1','class2'],['girls','subject']]
d. Print(class.loc['class 1','girls']
e. a and b
a. loc
b. at
C. iloc
d. <dataframe_object>.<column_lable>
180.Which of the following is correct syntax of using DataFrame() for defining a dataframe?
A. B.
ANSWER : A
182. Which of the following statement is incorrect for adding new column ‘sal’ in
dataframe emp?
a. emp.sal[200,300]
b. emp.loc[:, ‘sal’]=[200,300]
c. emp=emp.assign(‘sal’=[200,300])
d. emp.at[:,’sal’]=[200,300]
a. When values are assigned to column of dataframe for non-existing column, it will ad a new
column.
b. in iloc, like slices and index/positions is excluded when gives as start: end
c. while creating a dataframe from 2D Dictionary, keys of all inner dictionaries must be same in
number or name
ANSWER : (D) by default in drop function of dataframe object value of axis is 1 always.
184. Ritika has created one dataframe as given below:
Df=pd.DataFrame(dict1)
a. df.T
b. df.t
c. df.Transpose
d. df.Transpose()
A B
0 15 18
1 20 25
2 40 50
3 70 44
print(df[‘A’] =40)
a) A
0 15
1 20
2 40
3 70
b) A
2 40
c) A
0 False
1 Fals
a. When values are assigned to column of dataframe for non-existing column, it will ad a new
column.
b. in iloc, like slices and index/positions is excluded when gives as start: end
c. while creating a dataframe from 2D Dictionary, keys of all inner dictionaries must be same in
number or name
ANSWER : (D) by default in drop function of dataframe object value of axis is 1 always.
>>> print(series1)
a.
Output:
0 10
1 20
2 30
dtype: int64
b.
Output:
10
20
30
dtype: int64
c.
Output:
dtype: int64
d. None of the above
Ans. a
188.How many values will be there in array1, if given code is not returning any error?
>>> series4 = pd.Series(array1, index = [“Jan”, “Feb”, “Mar”, “Apr”])
a. 1
b. 2
c. 3
d. 4
Ans. d. 4
189.When we create a series from dictionary then the keys of dictionary become
________________
a. Index of the series
a 14
b 14
c 14
dtype: int64
b.
a 14
dtype: int64
c. Error
2 True
3 False
d) A
0 40
1 40
2 40
3 40
ANSWER : (D )
A. class.loc[‘class1’,:]
B. class.loc[:,’boy]
C. class.loc[‘class’]
D. class.loc[‘boy’]
192 Sandesh hs written following code to create dataframe with boolean index:
df.pd.DataFrame([1,2,3],index=[true,false,true])
When executing this code he is getting key error. Suggest him for correction:
194: Which of the following is not parameter of append() method used to merge two
dataframes?
a. Axis
b. Sort
c. Verify_integrity
d. Ignore_index
a. Union
b. Intersection
c. Product
d. Sum
a) i ii b) i ii c) A B C
A 10 50 A 10 NaN i 10 20 NaN
B 20 NaN B 50 NaN ii 20 NaN 30
C NaN 30 C NaN 30
d) A B C
i 10 50 NaN
ii 20 NaN 30
ANSWER: a
ANSWER: b
198. In the following questions two statements are given- one assertion denoted with(A) and
other reason denoted with(R). Select the correct answer as per given choices.
(a) For given dataframe
Rno name class
Sec1 1 ram 12
Sec2 2 hari 11
Sec3 3 akbar 12
(b)We can give axis=1 along with indexes/labels in drop method to delete column(s).
ANSWER: d
a. df.rename(index=[‘a’,’b,’,’c’], column={})
b. df.rename(index={‘A’:’a’,’B’:’b’,’C’:’c’})
c. df.rename(index=[‘A’=’a’,’B’=’b’,’C’=’c’])
d. df.rename(index=(‘a’,’b’,’c’))
ANSWER:b
a. b.
name code gender 102 448 504
102 jai 102 m name jai veer shera
448 veer 448 m code 102 448 504
504 shera 504 m gender m m m
ANSWER: a
a. 1
b. 2
c. 3
d. many
ANSWER: B) 2
202. To merge two dataframes df1 and df2 with no duplicate row labels, which command
should be executed?
a. df1.append(df2, ignore_index=True)
b. df1.append(df2, ignore_index=False)
c. df1.append(df2, verify_intigrity=True)
d. df1.append(df2, verify_intigrity=True)
203. For a dataframe with columns eno,ename and salary,shilpa want to add a new
column hra which will store 20% of salary.
Help her to do this.
a. df[‘hra’] = df[‘salary’]*0.2
b. df.hra = df.salary*0.2
c. df[‘hra’= df[‘salary’]*0.2]
d. None
ANSWER: A) Index
205. If admin wants to know the rate of book which code is 103 .What command
a. df[df[‘bcode’]=103]
b. df[‘bcode’ = 103]
c. df[df[‘bcode’]==103]
d. df[‘bcode’ == 103]
ANSWER: C) df[df[‘bcode’]==103]
207. Admin brought a book A1 of 510 rs and ask operator to store in dataframe the
command should operator write is
a.df.at[-1]=[106,'A1',510]
b.df.loc[-1]=[106,'A1',510]
c.df.loc[lan(df)]=[106,'A1',510]
d. Both a and b
Ans. c
208. Operator found that the rate of python is missing. So he thought to fill it with 500rs
what is the command.
a.df.replacena(500)
b.df.fillna=(500)
c.df.fillna(500)
d.df.replacena=(500)
Ans. c