0% found this document useful (0 votes)
67 views

12 IP Dataframe and Pyplot Notes

The document contains 20 questions related to pandas DataFrame. It covers basic concepts like creating, indexing and selecting data from DataFrame. It also includes some advanced operations like adding, subtracting and renaming columns in DataFrame. Long answer questions involve creating DataFrame, performing various operations on them and adding new columns.

Uploaded by

abesaale10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

12 IP Dataframe and Pyplot Notes

The document contains 20 questions related to pandas DataFrame. It covers basic concepts like creating, indexing and selecting data from DataFrame. It also includes some advanced operations like adding, subtracting and renaming columns in DataFrame. Long answer questions involve creating DataFrame, performing various operations on them and adding new columns.

Uploaded by

abesaale10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CHAPTER – DATAFRAME

VSA – Very Short Answer Question (for 1 Mark)


Q.1 DataFrame is dimensional data structure
Ans. two

Q.2 In DataFrame, is used for the row label.


Ans. Index

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.5 can also be known as subset section.


Ans. Indexing

Q.6 Boolean indexing helps us to select the data from the DataFrame using .
Ans. boolean vector.

Q.7 CSV file are the


Ans. Comma Separated Values.

Q.8 function is used to import a CSV file to DataFrame format.


Ans. read_CSV( )

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)

Q.16 State True / False:


A dataframe cannot be created using another dataframe.
Ans. False
Q.17 Which method is used to access vertical subset of a dataframe.?
(i) Iterrows()
(ii) Iteritems()
(iii) Itertuples()
Ans.(ii) Iteritems( )

Q.18 State whether True or False


a. A series object is size mutable.
b. A Dataframe object is value mutable
Ans. a. False
b. True

Q.19 Define the iterrows()


Ans. iterrows() returns the iterator yielding each index value along with a series containing the
data in each row.

Q.20 Which function is used to export DataFrame to a CSV file ?


Ans. To export a Pandas DataFrame to a CSV file, use to_CSV function.
Syntax : to_CSV(parameter)

SA – Short Answer Question (for 2 Marks)


Q.1What are the operation on Pandas DataFrame?
Ans. We can perform the following advanced operation on the DataFrame as
• Assignment
• Selection
• Pivoting
• Sorting
Aggregation

Q.2 Given the Output of the code


>>>import pandas as pd
>>>a= pd.DataFrame([1,1,1,None],index=[‘a’, ‘b’, ‘c’ , ‘d’], column = [‘One’])
>>>print(a)
Ans. One
a 1.0
b 1.0
c 1.0
d NaN

Q.3 Explain DataFrame. Can it be considered as 1D Array or 2D Array


Ans. DataFrame is two dimensional array with heterogeneous data usually represented in
tabular format. It can be considered as 2D array.
Q.4 Write the output of the following code
import pandas as pd
data=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]df
= pd.DataFrame(data)
print(df)
Ans. Output
0 a
1 b
2 c
3 d
4 e

Q.5 Write the output of the following code


import pandas as pd
data = [[‘Alex’,10], [‘Bob’,12], [‘Clarke’,13]]
df = pd.DataFrame(data,columns = [‘Name’ , ‘Age’])
print(df)
Ans. Name Age
0 Alex 10
1 Bob 12
0 Clarke 13

Q.6 Write the output of the following code


>>>import pandas as pd
>>>data = [[‘Alex’,10], [‘Bob’,12], [‘Clarke’,13]]
>>>df = pd.DataFrame(data,columns = [‘Name’ , ‘Age’],dtype=float)
>>>print(df)
Ans. Name Age
0 Alex 10.0
1 Bob 12.0
0 Clarke 13.0
Q.7 Write a Python code to create a dataframe with appropriate headings from the list given
below :
[‘S101’, ‘Amy’ ,70]
[‘S102’, ‘Bandhi’ ,69]
[‘S103’, ‘Cathy’ ,75]
[‘S104’, ‘Gundoho’ ,82]

Ans. import pandas as pd


data =[ [‘S101’, ‘Amy’ ,70],[‘S102’, ‘Bandhi’ ,69],[‘S103’, ‘Cathy’ ,75],[‘S104’, ‘Gundoho’ ,
df=pd.DataFrame(data,columns=[‘ID’, ‘NAME’, ‘MARKS’])
print(df)

LA – Long Answer Question (for 4 Marks/5 marks)


Q.1 Write the code in Pandas to create the following Data Frames.

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’))

Choose the correct option:


a. both (i) and(ii)
b. only(ii)
c. (i), (ii) and(iii)
d. (i), (ii) and(iv)
(iv) Which of the following command will display the column labels of the
DataFrame?
a. print(emp.columns())
b. print(emp.column())
c. print(emp.column)
d. print(emp.columns)
(v) Mr. Satvik Ahuja, the CEO wants to add a new column, the rating of the
performance of employees with the values, ‘A’, ‘A’, ‘B’, ‘A’, ‘B’, to the DataFrame. Help
him choose the command to do so:
a. emp.column=[’A’,’A’,’B’,’A’,’B’]
b. emp[‘Performance’]=[ ’A’,’A’,’B’,’A’,’B’]
c. emp.loc[‘Performance’]= [’A’,’A’,’B’,’A’,’B’]
d. Both (b) and (c) are correct
Ans.
(i) b. print(emp.max())

(ii) a.
emp1=emp[emp[‘empno’]==4]print(emp1)
d.emp1=emp[emp.empno==4] print(emp1)

(iii) a. both (i) and (ii)


(iv) d. print(emp.columns)
(v) b. emp[‘Performance’]=[ ’A’,’A’,’B’,’A’,’B’]

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.

Dataframe contents are Name


Age Score
0 Anu 26 87
1 Abhishek 25 67
2 Rajeev 24 89
3 Ritu 31 55
Name 4
Age 4
Score 4 dtype:
int64
OR
import numpy as npimport
pandas as pd
Cls1=pd.DataFrame({'Eng':[43,23,65,12],'Maths':[42,41,57,14],
'Hindi':[40,53,62,17]})
Cls2=pd.DataFrame({'Eng':[32,54,31,21],'Maths':[53,21,73,51],
'Hindi':[31,65,36,43]})
(i) print(Cls1.subtract(Cls2))
(ii) print(Cls1.add(Cls2))
(iii) Cls1.rename(columns={'Hindi':'Science'},inplace=True)
(iv) Cls1.rename(index={2:"Two",3:"Three"},inplace=True)
Q.5 Suppose a data frame contains information about student having columns
rollno, name, class and section.
Write the code for the following:
(i) Add one more column as fee
(ii) Write syntax to transpose data frame.
(iii) Write python code to delete column fee of data frame.
(iv) Write the code to append df2 with df1
(v) Display data of 1st to 3rd rows
Ans.
(i) Df1[‘fee’]=([100,200,300])
(ii) Df1=Df1.T
(iii) del Df1[‘fee’]
(iv) Df2=Df2.append(Df1)
(v) data.iloc[1:4]

CHAPTER – DATA VISUALIZATION


VSA – Very Short Answer Question (for 1 Mark)
Q.1 The matplotlib Python library developed by
Ans. John Hunter

Q.2 is amodule in the matplotlib package.


Ans. Pyplot

Q.3 The matplotlib API is imported using the .


Ans. standard convention

Q.4 The is bounding box with ticks and labels.


Ans. axes

Q.5 The can be plotted verticall or horizontally.


Ans. bar chart

Q.6 Histograms are used to show a/an .


Ans. distribution

Q.7 To add a tittle in a chart, function is used.


Ans. tittle()

Q.8 A bar graph uses bars to compare data among .


Ans. different categories.

Q.9 What is Pylab?


Ans. Pylab is a package that combine numpy,scipy ad matplotlib into a single namespace.

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( )

Q.11 How to import matplotlib?


Ans. form matplotlib import pyplot as plt.

Q.12 Which of the following is not a valid chart type?


a. line
b. bar
c. histogram
d. statistical
Ans. d.statistical

Q.13 The command used to show legends is


a. display()
b. show()
c. legend()
d. legends()

Ans. c.legend()

Q.14 The command used to give a heading to a graph is


a. plt.show()
b. plt.plot()
c. plt.xlabel()
d. plt.title()
Ans. d.plt.title()

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.17 In matplotlib, what is ticks?


Ans. A standard graph shows the marks on the axis, in matplotlib library, it is called ticks.

Q.18 What is the use of label in plotting?


Ans. Label is used to add labels or names to respective x and y axis.

Q.19 are specified as consecutive, non overlapping intervals of a variable, mainly


used in histograms.
i) Series
ii) Bins
iii) Gaps
iv) Axis

Ans. ii) Bins

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

Ans. Plt.title(‘New Graph’)

SA – Short Answer Question (for 3 Marks)


Q.1 Consider the following graph. Write the code to plot it.
Ans.
import matplotlib.pyplot as plta
= [0,1,2,3,4,5]
b = [10,31,26,24,20]
plt.plot(a,b)
plt.show()

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()

Q. 3 What will be the output of the follwing code ?


From matplotlib import pyplot as plt
X=[4,8,3]
Y=[1,6,9]
plt.plot(X,Y)
plt.title(‘Details)
plt.ylabel(‘Y axis’)
plt.xlabel(‘X axis’)
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()

You might also like