More Practice Questions For DataFrame
More Practice Questions For DataFrame
1. In given code dataframe ‘D1’ has ______ rows and ____ columns.
import pandas as pd
LoD = [{‘a’:10, ‘b’:20}, {‘a’:5, ‘b’:10, ‘c’:20}, {‘a’:7, ‘d’:10, ‘e’:20}]
D1 = pd.DataFrame(LoD)
a. 3, 3
b. 3, 4
c. 3, 5
d. None of the above
Ans. c.
Ans. c.
Ans. a. delete three columns having labels ‘Name’, ‘Class’ and ‘Rollno’
4. Which of the following are ways of indexing to access Data elements in a DataFrame?
a. Label based indexing
b. Boolean Indexing
c. All of the above
d. None of the above
8. In order to work with CSV files from Pandas, you need to import _____ , other than pandas.
a. csv
b. pandas
c. numpy
d. no extra package required
Ans : a) CSV
9. To read specific number of rows from a CSV file, which argument is to be givenin read_csv( ) ?
a. rows = <n>
b. nrows = <n>
c. n rows = <n>
d. number_rows = <n>
10. While reading from a CSV file, to use a column’s values as index labels, argumentgiven in
read_CSV( ) is:
a. index
b. index_col
c. index_values
d. index_label
Ans : b) index_col
Answer: C
CASE STUDY
1. Mr. Ankit is working in an organization as data analyst. He uses Python Pandas and Matplotlib for
the same. He got a dataset of the passengers for the year 2010 to 2012 for January, March and
December. His managerwants certain information from him, but he is facing some problems. Help
him by answering few questions given below:
Statement 2 # pd.DataFrame
MCQ TYPE QUESTIONS
1. 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]
2. The head() function of dataframe will display how may rows from top if no
parameter is passed.
(i) 1
(ii) 3
(iii) 5
(iv) None of these
Ans- (iii) 5
3. 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
(i) print(DF.head(1))
(ii) print(DF[0 : 1])
(iii)print(DF.iloc[0 : 1])
(iv)All of the above
(a) head( )
(b) head(5)
(c) top( )
(d) top(5)
8. ___________ method in Pandas can be used to change the index of rows and
columns of a Series or Dataframe
(a) rename( )
(b) reindex( )
(c) reframe( )
(d) none of these
1)
Assertion (A) : Dataframe has both a row and column index
Reason (R) : Dataframe is a two-dimensional labelled data structure like a table of Mysql.
Ans- (i) Both A and R are true and R is the correct explanation for A
2)
Assertion (A) : The shape attribute returns the number of rows and number of columns available in
dataframe.
Reason (R) : The shape attribute returns the values in the form of list.
3)
Assertion (A) : After running the following code:
df= pd.DataFrame([11,46],index=[‘True’,’False’] )
print(df[True] )
Reason (R) : Dataframe does not support Boolean indexing.
4)
Assertion (A) : We can add a new column in an existing dataframe using .at or .loc methods.
Reason (R) : When we assign new values to an existing column in a dataframe,the previous values
are overwritten.
Ans- (iii) Both A and R are true and R is the not correct explanation for A
5)
Assertion (A) : columns name in Dataframe can be change/rename.
Reason (R) : Dataframe the column name be changed through rename method.
Ans- (i) Both A and R are true and R is the correct explanation for A
a) print( df1.index)
b) print(df1.name)
c) print(df1.row)
d) print(df1.index,row=’values)
Ans- (a)
a) df1.rowdelete( )
b) Df1.del(np.nan)
c) Df1.drop(np.nan)
d) df1.dropna()
Ans- (d)
Ans- (c )
2. Riyaz is creating an application using pandas library in his program , his code is
mentioned below. Fill in the blanks to help him
Ans- (a)
II) Choose the right code from the following for the statement B.
Ans- (b)
III) Choose the right code from the following for the statement C.
Ans- (d )
3.
a) Choose the correct statement/ method for the required output: (5,3)
b) He wants to print the details of "January" month along with the number of
passengers, Identify the correct statement:
Month Passengers
0 Jan 25
2 Jan 35
i. df.loc[['Month','Passengers']][df['Month']=='Jan']
ii. df[['Month','Passengers']][df['Month']=='Jan']
iii. df.iloc[['Month','Passengers']][df['Month']=='Jan']
iv. df(['Month','Passengers']][df['Month']=='Jan')
Ans- (ii)