12 Ip Dataframes Notes
12 Ip Dataframes Notes
Q.5 Pandas provides _______ data structures for processing the data.
Ans. Two
Q.6 ________ function is used to add series and other, elements wise.
Ans. add()
Q.9 Given a Pandas series called Sample, the command which will display the last 3 rows
is_________.
Ans. print(Sample.tail(3))
Q.10 Given a Pandas series called Sequences, the command which will display the
first 4 rows is ____________.
Ans. print(Sequence.head(4))
Q.11 ____________ method in Pandas does not raise errors for multiple entries of a row,
column combinations.
Ans.pivot_table( )
29
Q.13 Given the following Series S1 and S2:
S1 S2
A 10 A 5
B 20 B 4
C 30 C 6
D 40 D 8
Write the command to find the multiplication of series S1 and S2
Ans. print(S1*S2)
30
SA – Short Answer Question (for 2 Marks)
Q.1 List two key features of Pandas.
Ans. The two features of Pandas are :
(i) It can process a variety of data set in different formats : time series, tabular
heterogeneous arrays and matrix data.
(ii) If facilitates loading and importing data from varied sources such as CSV and
DB/SQL.
Q5. Consider the following Series object, “company” and its profit in Crores
TCS 350
Reliance 200
L&T 800
Wipro 150
(i) Write the command which will display the name of the company having
profit>250.
(ii) Write the command to name the series as Profit.
31
SA – Short Answer Question (for 3 Marks)
Q. 1 Consider two objects a and b.
a is a list whereas b is a Series. Both have values 10,20,25,50.
What will be the output of the following two statements considering that the above objects
have been created already
a. print(a*2) b. print(b*2)
Justify your answer.
Ans.
a. will give the output as:
[10,20,25,50,10,20,25,50]
b. will give the output as
0 20
1 40
2 50
3 100
Justification: In the first statement a represents a list so when a list is multiplied by a number, it
is replicated that many number of times.
The second b represents a series. When a series is multiplied by a value, then each element of the
series is multiplied by that number.
32
CHAPTER – DATAFRAME
VSA – Very Short Answer Question (for 1 Mark)
Q.1 DataFrame is ___________ dimensional data structure
Ans. two
Q.3 ________________ is a general term for taking each item of something, one after another.
Ans. Iteration.
Q.4 _____________ function return last n rows from the object based on position.
Ans. tail( )
Q.6 Boolean indexing helps us to select the data from the DataFrame using_______.
Ans. boolean vector.
Q.9 Hitesh wants to display the last four rows of the dataframe df and has written the following
code :
df.tail( )
but last 5 rows are being displayed. Identify the errors and rewrite the correct code so that last 4
rows get displayed.
Ans. df.tail(4)
Q.10 Consider the following Python code and write the output for statement.
import pandas as pd
values=[“India”, “Canada”]
code=[“IND”, “CAN”]
df=pd.DataFrame(values,Index=Code,columns=[‘Country’]
Ans.
Code Country
IND India
CAN Canada
Q.11 The teacher needs to know the marks scored by the student with roll number 4. Help her to
identify the correct set of statement/s from the given options :
a. df1=df[df[‘rollno’]==4]
print(df1)
33
b. df1=df[rollno==4]
print(df1)
c. df1=df[df.rollno=4]
print(df1)
d. df1=df[df.rollno==4]
print(df1)
Ans.
a. df1=df[df[‘rollno’]==4]
print(df1)
d. df1=df[df.rollno==4]
print(df1)
Q.12
In Pandas the function used to delete a column in a DataFrame is
a. remove
b. del
c. drop
d. cancel
Ans. (b) del
Q.13 ____________ function applies the passed function on each individual data element of the
dataframe.
a. apply() b. applymap() c. pivot() d. pivot_table()
Ans. a. apply()
Q.14 Which of the following statement/s will give the exact number of values in
each column of the dataframe?
i. print(df.count())
ii. print(df.count(0))
iii. print(df.count)
iv. print(df.count(axis=’index’))
Choose the correct option:
a. both (i) and (ii)
b. only (ii)
c. (i), (ii) and (iii)
d. (i), (ii) and (iv)
Ans. a. both (i) and (ii)
Q.15 Which of the following command will display the column labels of the DataFrame?
a. print(df.columns()) b. print(df.column()) c. print(df.column) d. print(df.columns)
Ans. a. print(df.columns()) or d. print(df.columns)
34
Q.17 Which method is used to access vertical subset of a dataframe.?
(i) Iterrows()
(ii) Iteritems()
(iii) Itertuples()
Ans.(ii) Iteritems( )
35