0% found this document useful (0 votes)
2 views9 pages

More Practice Questions For DataFrame

The document consists of multiple choice questions, assertions, and case-based questions related to Python Pandas and data manipulation techniques. It covers topics such as DataFrame creation, indexing, reading CSV files, and various DataFrame operations. The document provides answers to each question, demonstrating knowledge of data analysis using Pandas.

Uploaded by

Daniel Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

More Practice Questions For DataFrame

The document consists of multiple choice questions, assertions, and case-based questions related to Python Pandas and data manipulation techniques. It covers topics such as DataFrame creation, indexing, reading CSV files, and various DataFrame operations. The document provides answers to each question, demonstrating knowledge of data analysis using Pandas.

Uploaded by

Daniel Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MULTIPLE CHOICE QUESTIONS

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.

2. D1[ : ] = 77 , will set ____ values of a DataFrame ‘D1’ to 77.

a. Only First Row


b. Only First Column
c. All
d. None of the above

Ans. c.

3. The following statement will


df = df.drop(['Name', 'Class', 'Rollno'], axis = 1)#df is a DataFrame object

a. delete three columns having labels ‘Name’, ‘Class’ and ‘Rollno’


b. delete three rows having labels ‘Name’, ‘Class’ and ‘Rollno’
c. delete any three columns
d. return error

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

Ans. c. All of the above

5. The following statement is


>>> DF=DF.rename({‘Maths’:’Sub1′,‘Science’:’Sub2′}, axis=’index’) #DF is a DataFrame

a. altering the column labels


b. altering the row and column labels (both)
c. Error
d. altering the row labels

Ans : b altering the column labels

6. Which of the following statement is Transposing the DataFrame ‘DF1’?


DF1.transpose
DF1.T
DF1.Trans
DF1.t
Ans. b. DF1.T

7. CSV stands for:


a. Comma Separated Values
b. Comma Separated Variables
c. Column Separated Values
d. Column Separated Variables

Ans a) Comma Separated Values

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>

Ans : b) nrows = <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

Assertion (A) and Reason (R)


Assertion (A): sorting is the operation to arrange data in a specific order ,sort_values ()function used
to perform the operation.
Reasoning (R): Row wise shorting cannot be performed in python dataframe objects

a. Both A and R are true and R is the correct explanation of A.


b. Both A and R are Ture and R is not the correct explanation of R .
c. A is True but R is false.
d. Both A and R are false
Answer : C

Assertion (A): We can read specific rows from a CSV file.


Reason (R): The nrows attribute of to_csv( ) is used to read specific rows from a CSV file.

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

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:

Code to create the above data frame:

import pandas as ______________ #Statement 1 data={"Year":


[2010,2010,2012,2010,2012],"Month":["Jan","Mar","Jan","Dec","Dec"],
"Passengers":[25,50,35,55,65]}
df=pd._______________ (data) #Statement 2
print(df)

Ans : Statement 1 # import pandas as pd

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]

Ans- (c) DF.iloc[6:10, 3:6]

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

Ans- (b) D.iloc

4. Which attribute of a dataframe is used to get number of axis?


a.T
b.Ndim
c.Empty
d.Shape

Ans- (b) Ndim

5. Display first row of dataframe ‘DF’

(i) print(DF.head(1))
(ii) print(DF[0 : 1])
(iii)print(DF.iloc[0 : 1])
(iv)All of the above

Ans- (iv)All of the above

6. To delete a row from a DataFrame, you may use


(a) remove
(b) del
(c) drop
(d) cancel
Ans- (c) drop
7. To get top 5 rows of a dataframe, you may use

(a) head( )
(b) head(5)
(c) top( )
(d) top(5)

Ans- (a) head( ) & (b) head(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

Ans- (b) reindex( )

REASON & ASSERTION QUESTIONS


Mark the correct choice as
i. Both A and R are true and R is the correct explanation for A
ii. Both A and R are true and R is not the correct explanation for A
iii. A is True but R is False
iv. A is false but R is True

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.

Ans- (iii) A is True but R is False

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.

Ans- (iii) A is True but R is False

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

CASE BASED QUESTIONS


1. Nidhi has created dataframe df1 as following , help her to perform following tasks and write the
code to help her to
df1

Student Marks Sports


I Abc 24.5 Cricket
II Def 27.5 Badminton
III Ghi Np.Nan Football

(i) Displays the index (row labels) of DataFrame

a) print( df1.index)
b) print(df1.name)
c) print(df1.row)
d) print(df1.index,row=’values)

Ans- (a)

(ii) Remove the null value rows

a) df1.rowdelete( )
b) Df1.del(np.nan)
c) Df1.drop(np.nan)
d) df1.dropna()

Ans- (d)

(iii) Returns True/False to show if the DataFrame is empty


a) Print(df1.nan)
b) Print(df1.null)
c) print(df1.empty)
d) print(df1.NULL)

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

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
I) Choose the right code from the following for statement A.
a) pandas b) df c) data d) pd

Ans- (a)

II) Choose the right code from the following for the statement B.

a) Dataframe b) DataFrame c) Series d) Dictionary

Ans- (b)

III) Choose the right code from the following for the statement C.

a) df.index b) df.shape( ) c) df.appenddf( ) d) df.concat( )

Ans- (d )

3.

Code to create the above data frame:


import pandas as ____________ #Statement 1
data={"Year":[2010,2010,2012,2010,2012],"Month":["Jan","Mar","Jan",
"Dec","Dec"] ,"Passengers":[25,50,35,55,65]}
df=pd.____________________(data) #Statement 2
print(df)

a) Choose the correct statement/ method for the required output: (5,3)

i. df.index ii. df.shape() iii. df.shape iv. df.size

Ans- (iii) df.shape

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)

You might also like