12 IP Dataframe and Pyplot Notes
12 IP Dataframe and Pyplot Notes
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)
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)
Df1 Df2
Mark1 Mark2 Mark1 Mark2
0 10 20 0 10 15
1 40 45 1 20 25
2 15 30 2 25 30
3 40 70 3 50 30
Write the commands to do the following operations on the DataFrames given below :
(i) To add DataFrames Df1 and Df2
(ii) To subtract Df2 from Df1
(iii) To Rename column Mark1 as Marks1 in both the DataFrame Df1 and Df2
(iv) To Change index label of Df1 from 0 to zero and from 1 to one.
Ans.
import numpy as npimport
pandas as pd
Df1=pd.DataFrame({‘Mark1’ :[10,40,15,40],’Mark2’ : [20,45,30,70]})
Df2=pd.DataFrame({‘Mark1’ :[10,20,25,50],’Mark2’ : [15,25,30,30]})
print(Df1)
print(Df2)
(i) print(Df1.add(Df2))
(ii) print(Df1.sub(Df2))
(iii) Df1.rename(columns={‘Mark1’ : ‘Marks1’}, inplace =True)
print(Df1)
(iv) Df1.rename(columns={0: ‘zero’,1: ‘one’}, inplace =True)
print(Df1)
Q.2 Consider the following DataFrame emp and answer the any four questions from (i) to (v)
Empno Name Dept Salary Experience
(in years)
1 Ram Singh IT 15000 2.5
2 Shyam Singh HR 18000 3
3 Nidhi Gupta IT 9000 2
4 Pooja Sharma EXE 24000 8
5 Rohan Malik HR 20000 6
(i) Write down the command that will give the following output.
Empno 5
Name Rohan MalikDept
HR
Salary 20000
Experience 6dtype:
object
a. print(emp.max)
b. print(emp.max())
c. print(emp.max(axis=1))
d. print(emp.max,axis=1)
(ii) CEO needs to know the salary of the employee with empno 4. Help him to
identify the correct set of statement/s from the given options:
a. emp1=emp[emp[‘empno’]==4]
print(emp1)
b. emp1=emp[emp]
print(emp1)
c. emp1=emp[emp.empno=4]
print(emp1)
d. emp1=emp[emp.empno==4]
print(emp1)
(iii) Which of the following statement/s will give the exact number of values in each
column of the dataframe?
i i. print(emp.count())
ii ii. print(emp.count(0))iii
iii. print(emp.count)
iv iv. print(emp.count(axis=’index’))
(ii) a.
emp1=emp[emp[‘empno’]==4]print(emp1)
d.emp1=emp[emp.empno==4] print(emp1)
Q.3 A dataframe fdf stores data about passengers, Flights and Years. First fews of the
dataframe are shown below.
Year Months Passengers
0 2009 January 112
1 2009 February 118
2 2009 March 132
3 2009 April 129
4 2009 May 121
Using the above DataFrame, Write commands for the following:
(a) Compute total passengers per Year
(b) Compute average passengers per Month.
Ans.
(i) fdf.pivot_table(index='year', value='passengers', aggfunc='sum')
(ii) fdf.pivot_table(index='month', values='passengers', aggfunc='mean')
Q.4 Give the output of the following code:
import numpy as npimport
pandas as pd
dict={'Name':pd.Series(['Anu','Abhishek','Rajeev','Ritu']),'Age':pd.Series([26,25,24,31]),
'Score':pd.Series([87,67,89,55])}
df=pd.DataFrame(dict)
print("Dataframe contents are")
print(df)
print(df.count())
OR
Write the code in Pandas to create the above dataframes and write the command to perform
following operations on the dataframes Cls1 and Cls2:
(i) To subtract Cls2 from Cls1.
(ii) To add Cls1 and Cls2.
(iii) To rename column Hindi as Science in Cls1.
(iv) To change the index label of Cls1 from 2 to two and from 3 to three.
Ans.
Q.10 Mr.Sanjay wants to plot a bar graph for the given set of values of subjects on x-axis and
number of students who opted for that subject on y-axis.
Complete the code to perform the following operation
(i) to plot the bar graph in statement 1
(ii) to display the graph in statement 2
x = [‘HINDI’, ‘ENGLISH’, ‘SCIENCE’ , ‘SST’]
y=[10,20,30,40]
# statement 1
# statement 2
Ans. (i) plt.bar(x,y)
(iii) plt.show( )
Ans. c.legend()
Q.15 Using Python Matplotlib can be used to count how many values fall
into each interval
a. line plot
b. bar graph
c. histogram
Ans. c. histogram
Q.16 Mr. Harry wants to draw a line chart using a list of elements named LIST. Complete
the code to perform the following operations :
(i) To plot a line chart using the given LIST
(ii) To give a y-axis label to the line chart named sample number.
import matplotlib.pyplot as PLINE
LIST=[10,20,30,40,50,60]
#statement 1
#statement 2
Ans. (i) PLINE.plot(LIST)
(iii) PLINE.ylabel(“Sample number”)
Q.20 Assuming that a line chart is plotted on x and y axis, write the command to give titleas
‘New Graph’ using Plt object
Q.2 Write code to draw the following bar graph representing the number of students in each
class.
Ans.
import matplotlib.pyplot as plt
Classes = ['VII','VIII','IX','X']
Students = [40,45,35,44]
plt.barh(classes, students)
plt.show()
Ans.
Q.4 Write the output graph of :
import matplotlib.pyplot as p
x=[2,3,4,5,6,7]
y=[1,2,3,4,5,6]
p.plot(x,y)
p.show()
Ans.
Q.5 Write code to plot a line graph showing the relation between channel name and its TRP
rating ( 4 channels). Include the titles and formatting of your choice. The font size of the x
and y labels should be 15 and font color should be green
Ans.
import matplotlib.pyplot as p
x=["Sony","Star","SAB","Zee"]
y=[60,40,55,35]
p.plot(x,y, linestyle=":")
p.title('TRP of various channels')
p.xlabel('Name of Channel',fontsize="15",color="green")
p.ylabel('TRP',fontsize="15",color="green")
p.show()
Q.6 Consider the following graph. Write a program in python to draw it along with proper
labeling of X-axis, Y-axis and Title for the line Chart of your choice.
Ans.
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-2, 2,50)
y=x*x
plt.plot(x,y)
plt.title('Y = x * x')
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.show()
Q.7 Consider the following graph. Write a program in python to draw it. (Height of Bars are
10,1,0,33,6,8)
Ans.
import numpy as np
import matplotlib.pyplot as plt
plt.hist([0,10,20,30,40,50],bins=[0,10,20,30,40,50,60],weights=[10,1,0,33,6,8],e
dgecolor='ye llow')
plt.title('Histogram of
Student Data')
plt.xlabel('value')
plt.ylabel('Frequency')
plt.show()