Marketing Sales
Age 25 24
Name Neha Rohit
Sex Female Male
>>>[Link]
Index (['Age',Name','Sex'],dtype='object')
>>>[Link]
Index (['Marketing','Sales'],dtype='object')
>>>[Link] 6
>>>[Link](3,2)
>>>[Link]
>>>dfn.ndim2
>>>dfn.T
Age Name Sex
Marketing 25 Neha Female
Sales 24 Rohit Male
15 Objective Question (1 Mark )
Q1. Complete the following code –
_____________________ #missing statement
D = {'code' : [102 , 104, 105 ], 'ename' : ['Arun', 'Geet', 'Amy'] }
df1 = [Link](D)
print(df1)
a) import pandas
b) import pandas as pp
c) import Pandas as pp
d) import pandas as pd
Ans b) import pandas as pp
Q2. Missing data in a Dataframe object is represented through –
a) NULL b) None
c) NaN d) <empty>
Ans c) NaN
Q3. The function to create a dataframe from a CSV file is –
a) to_csv() b)load_csv()
c) fetch_csv() d) read_csv()
PAGE:34
Ans d) read_csv()
Q4. Method or function to add a new row in a data frame is:
a. .loc() b. .iloc() c. join d. add()
Ans a. .loc()
Q5. While accessing the column from the data frame, we can specify the column name. In case
column does not exist, which type of error it will raise:
a. Key Error b. Syntax Error c. Name Error d. Runtime Error
Ans a. Key Error
Q6. What is a correct syntax to return the values of first row of a Pandas DataFrame? Assuming the
name of the DataFrame is dfRent.
a. dfRent[0] b. [Link][1] c. [Link][0] d. [Link][1]
Ans c. [Link][0]
Q7. Difference between loc() and iloc().:
a. Both are Label indexed based functions.
b. Both are Integer position-based functions.
c. loc() is label based function and iloc() integer position based function.
d. loc() is integer position based function and iloc() index position based function.
Ans c. loc() is label based function and iloc() integer position based function.
Q8. Which command will be used to delete 3 and 5 rows of the data frame. Assuming the data frame
name as DF.
a. [Link]([2,4],axis=0) b. [Link]([2,4],axis=1)
c. [Link]([3,5],axis=1) d. [Link]([3,5])
Ans a. [Link]([2,4],axis=0)
Q9. Which attribute is not used with DataFrame?
[Link]
c,[Link]
Ans [Link]
Q10. When we create a Dataframe from a list of Dictionaries the columns labels are formed by the
[Link] of the keys of the dictionaries
[Link] of the keys of the dictionaries
[Link] of the values of the dictionaries
[Link] of the values of the dictionaries
Ans [Link] of the keys of the dictionaries
Q11 The data of any CSV file can be shown in which of the following software?
[Link] Word
[Link]
c,Spreadsheet
[Link] of the above
Ans [Link] of the above
Q12 Statement that displays first 5 rows of a DataFramedf:
[Link][:5] [Link]()
[Link][:5] [Link] of these
Ans [Link] of these
Q13 Choose the correct function to rename city columns to location using rename() function:
a. [Link](columns={‘City’:’Location’})
b. [Link](columns={‘City’=’Location’})
c. [Link](‘City’=’Location’)
d. [Link]([Link](‘City’,’Location’))
PAGE:35
Ans a. [Link](columns={‘City’:’Location’})
Q14 Which attribute is used to transpose the Data Frame ?
a) Trans b)Transpose
c) T d) Tp
Ans c) T
Q15 Considering above DataFramedf which python command use to display all the records in the
reverse order.
a. print(df[::1])
b. print([Link][::-1])
c. print(df[-1:]+df[:-1])
d. print([Link]())
Ans b. print([Link][::-1])
05 Assertion and reason Based question ( 1 Mark )
Q1. Assertion – DataFrame is a two-dimensional Pandas structure, with ordered collections of
columns that can store data of different types.
Reason - Dataframe is an array-like structure with two indices or axes – row index ( axis = 0)
and column index (axis=1). Dataframe is value-mutable as well as size- mutable with
heterogeneous data.
a) Assertion is True & Reason is correct explanation of Assertion
b) Assertion is True, but Reason is partially True
c) Assertion is True but Reason is False
d) Both Assertion and Reason are False
Ans a) Assertion is True & Reason is correct explanation of Assertion
Q2. Assertion – Two basic data structure in Python are: Series and Dataframe. But both are different
from each other.
Reason - Series stores heterogenous data while Dataframe stores homogenous data.
a)Assertion is True & Reason is correct explanation of Assertion
b)Assertion is True, but Reason is partially True
c)Assertion is True but Reason is False
d)Both Assertion and Reason are False
Ans a) Assertion is True but Reason is False
Q3. Assertion (A):- DataFrame has both a row and column index.
Reasoning (R): - A DataFrame is a two-dimensional labelled data structure like a table of
MySQL.
a)Assertion is True & Reason is correct explanation of Assertion
b)Assertion is True, but Reason is partially True
c)Assertion is True but Reason is False
d)Both Assertion and Reason are False
Ans a) Both A and R are true and R is the correct explanation for A
Q4. Assertion: It is not possible to add a new row to an empty DataFrame, with no columns defined.
Reason: DataFrame does not support adding of new rows
a) Both the Assertion and the Reason are correct and the Reason is the correct explanation of the
Assertion.
PAGE:36
b) The Assertion and the Reason are correct but the Reason is not the correct explanation of the
Assertion.
c) Assertion is true but the Reason is false.
d) The statement of the Assertion is false but the Reason is True.
Ans c) Assertion is true but the Reason is false.
Q5. Statement 1 :-Labeled indexing uses rows and columns title to select data in the DataFrame
Statement 2 :- In boolean indexing, we will select rows or columns based on the actual values of
the data in the DataFrame
a. Statement 1 is valid but statement 2 is invalid
b. Statement 1 is invalid but statement 2 is valid
c. Both statement are valid
d. Both statements are invalid
Ans [Link] statements are valid.
05 Short Knowledge/Understanding/Application Based Questions (2 Marks)
Q1. Write a python code to create a dataframe as per given structure in the figure.
Country Rank
0 Russia 121
1 Colombia 40
2 Chile 100
3 Equador 130
4 Nigeria 11
Ans import pandas as pd
data = [Link]({'Country': ['Russia','Colombia','Chile','Equador','Nigeria'],
'Rank':[121,40,100,130,11]})
print(data)
Q2. The python code written below has syntactical errors. Rewrite the correct code and underline the
corrections made.
Import pandas as pd
df ={"IP":["Programming","MySQL","Networking"],"Marks":[30,30,10]}
df= [Link](df)
Print(df)
Ans import pandas as pd
df ={"IP":["Programming","MySQL","Networking"],"Marks":[30,30,10]}
df= [Link](df)
print(df)
Q3. Carefully observe the following code:
import pandas as pd
PAGE:37
Year1={'Q1':5000,'Q2':8000,'Q3':12000,'Q4': 18000}
Year2={'A' :13000,'B':14000,'C':12000}
totSales={1:Year1,2:Year2}
df=[Link](totSales)
print(df)
Answer the following:
i. List the index of the DataFramedf
ii. List the column names of DataFramedf.
Ans i. The index labels of df will include Q1,Q2,Q3,Q4,A,B,C
ii. The column names of df will be: 1,2
Q4. Consider the given DataFrame ‘Stock’:
Write suitable Python statements for the following
: i. Add a column called Special_Price with the following data: [135,150,200,440].
ii. Add a new book named ‘The Secret' having price 800.
Ans i. Stock['Special_Price']=[135,150,200,400]
ii. [Link]['4']=['The Secret',800]
Q5. What the following statements are doing?
(I) df['city']=['Gwalior','Indore','Agra','Dewas',’Gwalior’,’Indore’]
(II) [Link][2, : ]
Ans (i) creating a new column city with new data
(ii) getting all columns of row index 2
05 Short Knowledge/Understanding/Application Based Questions (3 Marks)
Q1. Write a Python code to create a DataFrame with appropriate column headings from the list given
below:
[[1001,'IND-AUS',’2022-10-17’], [1002,'IND-PAK',’2022-10-23’], [1003,'IND-SA' , ‘2022-10-30], [1004,'IND-
NZ',’2022-11-18’]]
Ans import pandas as pd
data=[[1001,'IND-AUS',’2022-10-17’], [1002,'IND-PAK',’2022-10-23’], [1003,'IND-SA' , ‘2022-
10-30], [1004,'IND-NZ',’2022-11-18’]]
df=[Link] ( data, columns = ['MatchID', 'TEAMS', 'DATE'] )
print(df)
Q2. Consider the given DataFrame ‘Items’:
PAGE:38
Name Price
Quantity
0 CPU 7750 15
1 Watch 475 50
2 Key Board 225 25
3 Mouse 150 20
Write suitable Python statements for the following:
i) Add a column called Sale Price which is 10% decreased value of Price
ii) Add a new item named “Printer” having price 8000 and Quantity as 10.
iii) Remove the column Quantity
Ans i) Items['Sale_Price']=0.90 * Items[‘Price’]
ii) [Link]['4']=[“Printer”, 8000, 10]
iii) Items=[Link]('Quantity', axis=1)
Q3. DataFrame ‘STU_DF’:
rollno name marks
0 115 Pavni 97.5
1 236 Rishi 98.0
2 307 Preet 98.5
3 422 Paul 98.0
Perform the following operations on the DataFramestuDF:
i. Add a new row in dataframe STU_DF with values [444,’karan’,88.0]
ii. Print no of rows and columns in dataframe STU_DF
iii. Delete row for rollno 307.
Ans i. STU_DF.loc[4,:]= [444,’karan’,88.0] or STU_DF.loc[4]= [444,’karan’,88.0]
ii. print(STU_DF.shape)
iii. STU_DF.drop(2, axis = 0)
PAGE:39
Q4. Write a Python code to create a DataFrame ‘Df’ using dictionary of lists for the following data.
Arna Ram Samrid
b it hi
Math 90 92 89
s
Scien 91 81 91
ce
Hindi 97 96 88
Ans import pandas as pd
D={'Arnab':[90,91,97],'Ramit':[92,81,96],'Samridhi':[89,91,88] }
Df=[Link](D,index=['Maths','Science','Hindi'])
print(Df)
Q5. Consider the following dataframendf as shown below :
Col1 Col2 Col3 Res
T1 62.893165 100.0 60.00 True
T2 94.734483 100.0 59.22 True
T3 49.090140 100.0 46.04 False
T4 38.487265 85.4 58.60 False
What will be the output produced by following statements :-
a. print( [Link] [ : , ’Col3’ : ] )
b. print( [Link][2 : , : 3] )
c. print( [Link] [ 1:3 , 2:3 ])
Ans a. Col3 Res
T1 60.00 True
T2 59.22 True
T3 46.04 False
T4 58.62 False
b. Col1 Col2 Col3
T3 49.090140 100.0 46.04
T4 38.487265 85.4 58.62
PAGE:40
c. Col3
T2 59.22
T3 46.04
05 Short Knowledge/Understanding/Application Based Questions (4 Marks)
Q1. Mr. Kapoor, a data analyst has designed the dataframe DF that contains data about Attendance
and number of classes of a week as shown below. Answer the following questions:
A. Predict the output of the following python statement:
a. print(DF[3: ])
b. print([Link])
B. Write Python statement to display the data of ‘No_of_classes’ column of
indexes ‘Tuesday’ to ‘Thursday’
OR (for option B only)
Write python statement to calculate No_of_classes * Atten and display it as Total attendance in a
day.
Ans A. (a)
(b)
B. print([Link][ 'Tuesday': 'Thursday', 'No_of_classes'])
OR
print("Total Attendance in a day:")
print(df['Atten'] * df['No_of_classes'])
Q2. Mr. Roshan, a data analyst has designed the DataFramedf that contains data about sales made by 4
salesmans in two half yearlys as shown below. Answer the following questions:
PAGE:41
First Second
Salesman1 23000 18000
Salesman2 11000 15000
Salesman3 60000 40000
Salesman4 35000 12000
A. Predict the output of the following python statement:
i. [Link]
ii. df[1:3]
B. Write Python statement to display the data of Second half yearly column of indexes
Salesman1 to Salesman3
OR (Option for part iii only)
Write Python statement to compute and display the sum of data of First column and Second column of
the above given DataFrame.
Ans A i. (4, 2)
i. df[1:3]
First Second
Salesman2 11000 15000
Salesman3 60000 40000
B. print([Link]['Salesman1': 'Salesman3', 'Second'])
OR
print([Link]+[Link])
Q3. Mr. Sharma, a data analyst has designed the df that contains data about Cyber Olympiad
details with ‘Cyb1’, ‘Cyb2’, ‘Cyb3’, ‘Cyb4’, ‘Cyb5’ as indexes shown below.
Answer the following questions:
School Total_students Topper Runnerup
Cyb1 KVS 50 46 4
Cyb2 NVS 45 35 10
PAGE:42
Cyb3 DPS 30 20 10
Cyb4 MPS 25 18 7
Cyb5 GPS 37 28 9
Cyb6 BPS 33 25 8
A. Predict the output of the following –
i. [Link]
ii. df.[2:4]
B. Write python statement to display the data of the topper column of index Cyb2 to
Cyb5.
OR
Write Python statement to compute and display the difference of data of Total_students column
and Runnerup column of the above given DataFrame.
Ans A. Output:
i. (5,4)
ii.
School tot_students Topper Runnerup
Cyb2 GPS 20 18 2
Cyb4 MPS 18 10 8
B. Python statement: print([Link]['Cyb2':
'Cyb5','Topper'])
OR
A. print(df.Total_students-[Link])
Q4. Consider the following Data Frame sports:
ID NAME GENDER
SD1 1 ARUNA F
SD2 2 RAHUL M
SD3 3 SAKSHI F
SD4 4 RAMAN M
(A)
i. Predict the output of the following python statement: sports. Shape
ii. Write command to Add a new row with values(5, SAM, M):
(B) Write python statement to delete column Gender.
PAGE:43
OR (Option for part iii only)
Write python statement to delete the row with index SD3.
Ans A. Output
i. (4,3)
ii [Link][‘SD5’]=[5, ‘RAM’, ‘M’]
B. del Sports[‘GENDER’]
OR
[Link](index= ‘SD3’))
2 mark for correct python statement
Q5. 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 manager wants certain information from him, but he is facing some
problems. Help him by answering few questions given below:
A. Predict the output of the following python statement:
i. [Link](3)
ii. df[df. Passengers>50]
[Link] Python statement to display the data of year column of indexes 1 to 3.
OR (Option for part iii only)
Write the Python code to rename the name of the column name is ‘mon_name’in place of
“Month” in the above Dataframe.
Ans A)
. i) 2 2012 Jan 35
3 2010 Dec 55
4 2012 Dec 65
ii) 55 65
B )Python statement: print([Link][1: 3, 'year'])
PAGE:44
OR
[Link](columns={‘Month’:’mon_name’)
05 Case Based Questions (5 Marks)
Q1. Mr. Ankit is working in an organisation 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 manager wants 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)
i. Choose the right code from the following for statement 1.
i. pd ii. df iii. data iv. p
ii. Choose the right code from the following for the statement 2.
i. Dataframe ii. DataFrame iii. Series iv. Dictionary
iii. Choose the correct statement/ method for the required output: (5,3)
i. [Link]. [Link]()
iii. [Link]. [Link]
iv. He wants to print the details of "January" month along with the number of passengers,
Identify the correct statement:
PAGE:45
`
i. [Link][['Month','Passengers']][df['Month']=='Jan']
ii. df[['Month','Passengers']][df['Month']=='Jan']
iii. [Link][['Month','Passengers']][df['Month']=='Jan']
iv. df(['Month','Passengers']][df['Month']=='Jan')
v. v. Mr. Ankit wants to change the index of the Data Frame and the output for the same is given
below. Identify the correct statement to change the index..Mr. Ankit wants to change the index
of the Data Frame and the output for the same is given below. Identify the correct statement to
change the index.
i. [Link][]=["Air India","Indigo","Spicejet","Jet","Emirates"]
ii. [Link]["Air India","Indigo","Spicejet","Jet","Emirates"]
iii. [Link]=["Air India","Indigo","Spicejet","Jet","Emirates"]
iv. [Link]()=["Air India","Indigo","Spicejet","Jet","Emirates"]
Ans Answer: (i) pd
Answer: (ii) DataFrame
Answer: (iii) [Link]
Answer: (iv) df[['Month','Passengers']][df['Month']=='Jan']
Answer: (v) [Link]=["Air India","Indigo","Spicejet","Jet","Emirates"]
Q2. Sanyukta is the event incharge in a school. One of her students gave her a suggestion to use
Python Pandas andMatplotlib for analysing and visualising the data, respectively. She has
created a Data frame “SportsDay” to keeptrack of the number of First, Second and Third prizes
PAGE:46
won by different houses in various events.
i. Display the house names where the number of Second Prizes are in the range of 12 to 20.
a. df['Name'][(df['Second']>=12) and (df['Second']<=20)]
b. df[Name][(df['Second']>=12) & (df['Second']<=20)]
c. df['Name'][(df['Second']>=12) & (df['Second']<=20)]
d. df[(df['Second']>=12) & (df['Second']<=20
ii. Display all the records in the reverse order.
a. print(df[::1])
b. print([Link][::-1])
c. print(df[-1:]+df[:-1])
d. print([Link]())
iii. Display the bottom 3 records.
a. [Link](3)
b. [Link](3)
c. [Link](3)
d. [Link](3)
iv. Choose the correct output for the given statements: x=[Link][:1] print(x)
a. 0 b. Name
c. First d. Error
v. Which command will give the output 24:
a. print([Link]) b. print([Link])
PAGE:47
c. print([Link]) d. print([Link])
Ans Answer: c. df['Name'][(df['Second']>=12) & (df['Second']<=20)]
Answer: b. print([Link][::-1])
Answer: d. [Link](3)
Answer: b. Name
Answer: a. print([Link])
Q3. Naman has created the following dataframe “Climate” to record the data about climatic
conditions of four years.
i. Which of the following code snippets will return the MaxTemp and Rainfall for year 2018 and
2019?
a. Climate[['MaxTemp','Rainfall']][1:3]
b. Climate['MaxTemp', 'Rainfall'][1:3]
c. [Link][1:3]
d. [Link][1:3,1:2]
ii. Display the temperature difference between MaxTemp and MinTemp for all the rows in the
dataframe Climate.
a. Climate=Climate["MaxTemp"]-Climate["MinTemp"]
b. print(Climate["maxt"]-Climate["mint"])
c. print(Climate["MaxTemp"]-Climate["MinTemp"])
d. print([Link]["MaxTemp"]-Climate["MinTemp"])
iii. To display 2 rows from the top in the dataframe, which of the following statement is correct:
a. print ([Link]()=2 )
b. print ([Link](n==2) )
c. print ([Link](range(2)) )
d. print ([Link](2) )
PAGE:48
.
iv. Which of the following statement/s will give the exact number of values in each column of
the dataframe?
a. print([Link]())
b. print([Link](0))
c. print([Link])
d. print([Link](axis='index'))
Choose the correct option:
a) both (a) and (b)
b) only (b)
c) (a), (b) and (c)
(d) (a), (b) and (d)
v. To display 2 rows from the bottom in the dataframe, which of the following statement is
correct:
a. print ([Link]()=2 )
b. print ([Link](n==2) )
c. print([Link](range(2)))
d. print ([Link](2) )
Ans Answer: (c) [Link][1:3]
Answer : (c) print(Climate["MaxTemp"]-Climate["MinTemp"])
Answer: (d) print ([Link](2))
Answer: (d) (a), (b) and (d)
Answer: (d) print ([Link](2) )
Q4. HR Department of ABCTech has created following dataframe to store data about salaries and
bonus paid to their employees.
import pandas as pd
import numpy as np
PAGE:49
d1={'EName':[ 'Kavita', 'Sudha', 'Garima’]'],'Sal':[50000,60000,55000],
'Bonus':[3000,4000,5000]}
Df1=[Link](d1)
Choose the python statement using suitable functions for the following tasks:
i. Display the columns Sal and Bonus
a. df1 [:Sal :Bonus]
b. [Link](['Sal','Bonus'])
c. [Link](['Sal','Bonus'])
d.df1[['Sal','Bonus']]
ii. Display the details of employee Kavita.
a. df1[[Link]='Kavita']
b. [Link][[Link]=='Kavita']
c. [Link][[Link]=='Kavita']
d. df1[EName='Kavita']
iii. Display the details of the last employee.
a. [Link](1) b. [Link] (-1)
c. [Link](n=1) d. [Link]()
iv. Add a new column named ‘Email’ with the value “abc@[Link]”.
a. Df1[‘Email’]= ‘abc@[Link]’
b. Df1[Email]=’abc@[Link]’
c. [Link][‘Email’]=’abc@[Link]’
d. Df1(‘Email’)=’abc@[Link]’
v. Write a python statement to print the details of employees having Sal more than 50000
a. [Link]>=5000
b. df1[[Link]>=5000]
c. df1[df1.'Sal'>=5000]
PAGE:50
[Link][[Link]>=5000]
Ans Answer: (b) [Link](['Sal','Bonus'])
Answer (b) [Link][[Link]=='Kavita']
Answer: (a) [Link](1)
Answer : (a) Df1[‘Email’]= ‘abc@[Link]
Answer: (b) df1[[Link]>=5000]
Q5. Zeenat has created the following data frame dataframe1 to keep track of data Rollno, Name,
Marks1 and Marks2 for various students of her class where row indexes are taken as the
default values:
i. Which among the following option will give 90, 95 as output
a) print(max(dataframe1['Marks1','Marks2'])
b) print(([Link](),([Link]())))
c) print(max(dataframe1['Marks1'])
d) print(max(dataframe1['Marks2'])
ii. She needs to know the marks scored by Rollno 2. Help her to identify the correct set of
statement/s from the given options:
a. print(dataframe1[dataframe1['Rollno']= =2])
b. print(dataframe1['Rollno']= =2)
c. print(dataframe1[dataframe1. Rollno = =2])
d. print(dataframe1[dataframe1['Rollno']])
iii. Which of the following statement/s will delete the 3rd column?
a. del dataframe1['Marks1']
b. [Link]('Marks1')
PAGE:51
c. drop dataframe1['Marks1']
d. pop dataframe1['Marks1']
Choose the correct option:
a) both (a) and (b)
b) only (b)
c) (a), (b) and (c)
d) (a), (b) and (d)
iv. Which of the following command will display the total number of elements in the
dataframe?
a. print([Link])
b. print([Link])
c. print([Link])
d. print([Link])
v. Now she wants to add a new column Marks3 with relevant data.
Help her choose the command to perform this task.
a. [Link]=[ 45,52,90,95]
b. dataframe1 ['Marks3']= [ 45,52,90,95]
c. [Link]['Marks3']= [ 45,52,90,95]
d. Both (b) and (c) are correct
Ans Answer: (b) print(([Link](),([Link]())))
Answer : (c) print(dataframe1[dataframe1. Rollno = =2])
Answer : (a) both (a) and (b)
Answer (c) print([Link])
Answer: (b) dataframe1 ['Marks3']= [ 45,52,90,95]
PAGE:52