0% found this document useful (0 votes)
3 views6 pages

Ch-2 Python Worksheets

The document consists of multiple worksheets focused on data handling using Pandas and Matplotlib in Python. It includes questions on creating DataFrames, manipulating data, and generating plots, along with coding exercises to correct errors and complete code snippets. The worksheets cover various topics such as CSV files, Series, DataFrames, and visualizations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Ch-2 Python Worksheets

The document consists of multiple worksheets focused on data handling using Pandas and Matplotlib in Python. It includes questions on creating DataFrames, manipulating data, and generating plots, along with coding exercises to correct errors and complete code snippets. The worksheets cover various topics such as CSV files, Series, DataFrames, and visualizations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Ch-1 Data Handling Using Pandas – I & Matplotlib

Worksheet No.1
Q1. State whether the following statement is True or False :
In Python, we cannot create an empty DataFrame.
Q2. What is the default index type for a Pandas Series if not explicitly specified?
(A) String (B) List (C) Numeric (D) Boolean
Q3. In Python which function of matplotlib library is used to save a plot ?
(A) save() (B) saveplot() (C) export() (D) savefig()

Q4. Which of the following data structures is used for storing one-dimensional labelled data in Python Pandas?
(A) Integer (B) Dictionary (C) Series (D) DataFrame
Q5. Which of the following Python statements will be used to select a specific element having index as points, from a
Pandas Series named ser?
(A) [Link](points) (B) [Link] (points) (C) ser[points] (D) [Link][points]

Q6. Which of the following libraries defines an ndarray in Python ?


(A) pandas (B) numpy (C) matplotlib (D) scipy
Q7. Which of the following Python statements is used to change a column label in a DataFrame, df?
(A) df = [Link]({old_name: new_name}, axis='columns')
(B) df = [Link](old_name, new_name), axis='columns'
(C) df = df.change_name(old_name, new_name, axis='bar')
(D) df [Link]({old_name: new_name}, axis='bar')
Q8. In Python Pandas, DataFrame .______ [] is used for label indexing with DataFrames.
(A) label (B) index (C) labindex (D) loc
Q9. (a) Mention any two main points of difference between Series and DataFrame of Python Pandas.
(b) Explain how we can access elements of a series using slicing. Give an example to support your answer.
Q10. (a) Rohit is trying to create a Pandas Series from scalar values. His code has some mistakes. Rewrite the correct
code and underline the corrections made.
import pandas
data = [50, 15, 40]
series = [Link] (data, Index=['x', 'y', 'z'])
Print(series)
(b) Complete the given Python code to generate the following output:
COLOUR NAME QTY
0 Red Apple 10
1 Blue Berry 15
2 Green Guava 20
import _______ as pd
data=[{'COLOUR': 'Red', 'NAME': 'Apple', 'QTY':10},
{'COLOUR': 'Blue', 'NAME': ' Berry', ' QTY':15},
{________, 'NAME' : ' Guava', 'QTY':20}]
[Link] (__________)
print (________)
Q11. (a) Write a Python program to create the following DataFrame using a Dictionary of Series:
City State
0 Mumbai Maharashtra
1 Dehradun Uttarakhand
2 Bengaluru Karnataka
3 Hyderabad Telangana
(b) Write a Python program to create a Pandas Series as shown below from an ndarray containing the numbers 10,
20, 30, 40, 50 with corresponding indices 'A', 'B', 'C', 'D', 'E'.
A 10
B 20
C 30
D 40
E 50
Q12. Gurkirat has to fill in the blanks in the given Python program that generates a line plot as shown below. The
given
line plot represents the temperature (in degree Celcius) over five days as given in the table: 4
Days Temperature
Day 1 30
Day 2 32
Day 3 31
Day 4 29
Day 5 28

import _______as plt # Statement-1


days = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
temp = [30, 32, 31, 29, 28]
plt. ____ (days, temp) # Statement-2
[Link]('_______ ') # Statement-3
[Link]('Temperature')
[Link]('_______') # Statement-4
[Link]()
Write the missing statements according to the given specifications:
(i) Write the suitable code to import the required module in the blank space in the line marked as Statement-1.
(ii) Fill in the blank in Statement-2 with a suitable Python function name to create a line plot.
(iii) Refer to the graph shown and fill in the blank in Statement-3 to display the appropriate label for x-axis.
(iv) Refer to the graph shown and fill in the blank in Statement-4 to display the suitable chart title.

Q13. Consider the DataFrame Doctor shown below: 5


DID Name Department Fee
0 101 Dr. Joe ENT 1500
1 102 Dr. Salma UROLOGY 1600
2 103 Dr. Jeet ORTHO 1550
3 104 Dr. Neha ENT 1200
4 105 Dr. Vikram ORTHO 1700
Write suitable Python statements for the following:
(i) To print the last three rows of the DataFrame Doctor.
(ii) To display the names of all doctors.
(iii) To add a new column 'Discount' with value of 200 for all doctors.
(iv) To display rows with index 2 and 3.
(v) To delete the column 'Department'
Ch-1 Data Handling Using Pandas – I & Matplotlib
Worksheet No.2
Q1. Which of the following is NOT true with respect to CSV files?
(A) Values are separated by commas.
(B) to_cvs() can be used to save a dataframe to a CSV file.
(C) CSV file is created using a word processor.
(D) CSV file is a type of text file
Q2. What will be the output of the following Python code?
import pandas as pd
dd={'One' :1, 'Two' :2, 'Three' 3, 'Seven' :7}
rr=pd. Series (dd)
rr ['Four'] = 4
print (rr)

Q3. Which of the following cammand will not show first five rows from the Pandas series named S1?
(A) S1 [0 : 5] (B) [Link] ()
(C) [Link] (5) (D) [Link] [0 : 5]
Q4. The Python code written below has syntactical errors. Rewrite the correct code and underline the correction(s)
made.
import Pandas as pd
stud=['Name' : 'Ramya', 'Class' : 11, 'House' : 'Red']
s=p. Series (s)
print (s)

Q5. Find the output of the following Python code:


import pandas as pd
com=[Link]([45,12,15,200],index=['mouse','printer', 'webcam' , 'keyboard'])
print (com[1 : 3])
Q6. Consider the following Python code:
import pandas as pd
S1=pd. Series ([ 'Rubina', 'Jaya', 'Vaibhav'],index=[10, 16, 18])
S2=pd. Series (........., index=[10, 16 18])
S3=pd. Series ([56, 67, 86],............)
xiia={'Name' :.........., 'Subject' : S2, 'Marks' : S3}
df=pd. DataFrame (.........)
print (df)
Complete the above Python code to display the
following output:
Name Subject Marks
10 Rubina IP 56
16 Jaya HSc 67
18 Vaibhav Ip 86
Q7. Kabir, a data analyst, has stored the voter's name and age in a dictionary. Now, Kabir wants to create a list of
dictionaries to store data of multiple voters.
He also wants to create a DataFrame from the same list of dictionaries having appropriate row labels as shown below:
Voter_Name Voter_Age
Ar1001 Arjun 35
Ba3002 ala 23
Go4002 Govind 25
Dh4007 Dhruv 19
Na6005 Navya 18
Help Kabir in writing a Python program to complete the task.

Q8. Consider the given DataFrame 'password':


CodeName Category Frequency
0 aaaaaa alpha 6.91
1 dragon nimal 18.52
2 baseball sport 1.29
3 football sport 11.11
4 monkey animal 3.72
5 qwerty alpha 1.85
6 abcde alpha 3.19
Write suitable Python statements for the following:
(i) To add a new row with following values:
CodeName – 'abc123'
Category – alphanumeric
Frequency – 12.8
(ii) To delete the row with the row label 2.
(iii)To delete the column having column label as Frequency.
Q9. Ms. Ritika conducted an online assessment and stored the details in a DataFrame result as given
below:
Name Score Attempts Qualify
a Atulya 12.5 1 yes
b Disha 9.0 3 no
c Kavita 16.5 2 yes
d John 5.0 1 no
Answer the following questions:
(i) Predict the output of the following Python statement:
print ([Link] [: , 'Attempts'] > 1)
(ii) Write python statement to display the last three records.
(iii)Write python statement to display records of 'a' and 'd' row labels.
(iii)Write suitable Python statement to retrieve the data stored in the file, '[Link]' into a DataFrame, 'regis'.
Q10. (a) The inventory mangement software of a grocery shop stores the price of all fruits as follows:
Fruits=[ 'Apple' , 'Guava' , 'Papaya' , 'Grapes' ,'Mango']
Price=[150, 40, 50, 30, 120]
Write suitable Python code to generate a Bar Chart on the given data. Also add the chart title and label for X and Y axis.
Also add suitable statement to save this chart with the name fruits . png.
(b) Write suitable Python code to draw the following line chart "CO2 Emission" having title and label for X and Y axis as
shown below.
Month wise CO2 emission

Also give suitable Python statement to save this chart with the name, [Link]
Ch-1 Data Handling Using Pandas – I & Matplotlib
Worksheet No.3

Q1. CSV stands for:


i. Column Separated Value
ii. Class Separated Value
iii. Comma Separated Value
iv. Comma Segregated Value
Q2. Which of the following command will show the last 3 rows from a Pandas
Series named NP?
i. [Link]( )
ii. [Link](3)
iii. [Link](3)
iv. All of the above
Q3. In Python Pandas, while performing mathematical operations on series, index
matching is implemented and all missing values are filled in with _____by
default.
i. Null
ii. Blank
iii. NaN
iv. Zero
Q4. The python code written below has syntactical errors. Rewrite the correct code and underline the corrections
made.
Import pandas as pd
df ={"Technology":["Programming","Robotics","3D Printing"],"Time(in months)":[4,4,3]}
df= [Link](df)
Print(df)

Q5. Predict the output of the given Python code:


import pandas as pd
list1=[-10,-20,-30]
ser = [Link](list1*2)
print(ser)
Q6. Complete the given Python code to get the required output as: Rajasthan
import _________ as pd
di = {'Corbett': 'Uttarakhand', 'Sariska':'Rajasthan', 'Kanha': 'Madhya Pradesh’, 'Gir':'Gujarat'}
NP = ___________. Series( _____ )
print(NP[ ___________ ])

Q7. Create a DataFrame in Python from the given list:


[[‘Divya’,’HR’,95000],[‘Mamta’,’Marketing’,97000],[‘Payal’,’IT’,980000],[‘Deepak’,’Sales’,79000]]
Also give appropriate column headings as shown below:
Name Department salary
0 Divya HR 95000
1 Mamta Marketing 97000
2 Payal IT 98000
3 Deepak Sales 79000
Q8. Consider the given DataFrame ‘Genre’:
Type Code
0 Fiction F
1 Non Fiction NF
2 Drama D
3 Poetry P
Write suitable Python statements for the following:
i. Add a column called Num_Copies with the following data:
[300,290,450,760].
ii. Add a new genre of type ‘Folk Tale' having code as “FT” and 600
number of copies.
iii. Rename the column ‘Code’ to ‘Book_Code’.

Q9. Ekam, a Data Analyst with a multinational brand has designed the DataFrame
df that contains the four quarter’s sales data of different stores as shown below:
Store Qtr1 Qtr2 Qtr3 Qtr4
0 Store1 300 240 450 230
1 Store2 350 340 403 210
2 Store3 250 180 145 160
Answer the following questions:
i. Predict the output of the following python statement:
a. print([Link])
b. print(df[1:3])
ii. Delete the last row from the DataFrame.
iii. Write Python statement to add a new column Total_Sales which is the addition of all the 4 quarter sales.
OR
(Option for part iii only)
Q10. Write Python statement to export the DataFrame to a CSV file named [Link] stored at D: drive.

The heights of 10 students of eighth grade are given below:


Height_cms=[145,141,142,142,143,144,141,140,143,144]
Write suitable Python code to generate a histogram based on the given data, along with an appropriate chart title and both
axis [Link] give suitable python statement to save this chart.
OR
Write suitable Python code to create 'Favourite Hobby' Bar Chart as shown below:

You might also like