0% found this document useful (0 votes)
8 views61 pages

Python Data Structures and Operations

The document contains a series of practical questions and corresponding Python code examples related to data manipulation using pandas. It covers creating and manipulating Series and DataFrames, including operations like indexing, slicing, sorting, and updating values. Each practical question is followed by sample outputs demonstrating the results of the code.

Uploaded by

JLG
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)
8 views61 pages

Python Data Structures and Operations

The document contains a series of practical questions and corresponding Python code examples related to data manipulation using pandas. It covers creating and manipulating Series and DataFrames, including operations like indexing, slicing, sorting, and updating values. Each practical question is followed by sample outputs demonstrating the results of the code.

Uploaded by

JLG
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

IP Practical File 2023-24

Practical Question - 1

Study the following series -


1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
And create the same using
(i) ndarrays (ii) Dictionary

Python Code

Question (i)

import pandas as pd
import numpy as np

days =
['Sunday','Monday','Tuesday','Wednesday','Thursday']

array = [Link](days)
s1 = [Link] (array, index = [1, 2, 3, 4, 5])

print (s1)
Question (ii)

import pandas as pd

dict1 = {1:'Sunday', 2:'Monday', 3:'Tuesday',


4:'Wednesday', 5:'Thursday'}

s2 = [Link] (dict1)

print (s2)

Outputs

Question (i) Question (ii)


1 Sunday 1 Sunday
2 Monday 2 Monday
3 Tuesday 3 Tuesday
4 Wednesday 4 Wednesday
5 Thursday 5 Thursday
dtype: object dtype: object
Practical Question - 2

A series that stores the average marks scored by 10 students is as follows –


[90, 89, 78, 91, 80, 88, 95, 98, 75, 97]
Write a code to :
(i) Create a series using the given dataset with index values (1-10) generated
using arange ( ).
(ii) Give name to the series as ‘AVERAGES’ and index values as ‘ROLL
NUMBER’.
(iii) Display the top three averages.
(iv) Display all mark averages less than 80.
(v) Update the mark averages of roll number (index) 5 to 82 and display the
series.
(vi) Display mark detail of roll number 7, 8 and 9.

Python Code

import pandas as pd ; import numpy as np

dataset = [90, 89, 78, 91, 80, 88, 95, 98, 75, 97]
index_array = [Link](1, 11, 1)

s1 = [Link] (dataset, index = index_array)

print (s1) #Q(i)

[Link] = 'AVERAGES' ; [Link] = 'ROLL NUMBER'

print (s1) #Q(ii)

print ([Link](3)) #Q(iii)

print(s1[s1<80]) #Q(iv)

s1[5] = 82; print (s1) #Q(v)

print([Link][7 : 10]) #Q(vi)

Outputs

Question (i) Question (ii) Question (iii)


1 90 ROLL NUMBER ROLL NUMBER
2 89 1 90 1 90
3 78 2 89 2 89
4 91 3 78 3 78
5 80 4 91
6 88 5 80 Name:
7 95 6 88 AVERAGES,
8 98 7 95 dtype: int64
9 75 8 98
10 97 9 75
dtype: int64 10 97
Name: AVERAGES,
dtype: int64

Question (iv) Question (v) Question (vi)


ROLL NUMBER ROLL NUMBER ROLL NUMBER
3 78 1 90 7 95
9 75 2 89 8 98
Name: AVERAGES, 3 78 9 75
dtype: int64 4 91 Name:
5 82 AVERAGES,
6 88 dtype: int64
7 95
8 98
9 75
10 97
Name: AVERAGES,
dtype: int64
Practical Question - 3

Write a program to store employees’ salary data of one year. Write a code to
do the following :
Salary_data = [120000, 120000, 130000, 115000, 300000,
150000, 100000, 250000, 160000, 400000, 250000, 350000]

Index = Jan, Feb, March, April, May, June, July, Aug,


Sep, Oct, Nov, Dec
(i) Display salary data by slicing in 4 parts.
(ii) Display salary of any April month.
(iii) Apply increment of 10% into salary for all values.
(iv) Give 2400 arrear to employees in April month.

Python Code

import pandas as pd

salary_data = [120000, 120000, 130000, 115000, 300000,


150000, 100000, 250000, 160000, 400000, 250000, 350000]
index_data = ['Jan', 'Feb', 'March', 'April', 'May',
'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

s1 = [Link] (salary_data, index = index_data)

print (s1)

print ("First Quarter Salary Details : ")

print ([Link]['Jan' : 'March'])

print ("Second Quarter Salary Details : ")

print ([Link]['April' : 'June'])

print ("Third Quarter Salary Details : ")

print ([Link]['July' : 'Sep'])

print ("Fourth Quarter Salary Details : ")

print ([Link]['Oct' : 'Dec'])

#Q(ii)

print ([Link]['April':'April'])

#Q(iii)

s2 = s1 + (s1*0.1); print (s2)

#Q(iv)

[Link]['April'] = [Link]['April'] + 2400

print (s1)

Outputs
Original Series

Jan 120000
Feb 120000
March 130000
April 115000
May 300000
June 150000
July 100000
Aug 250000
Sep 160000
Oct 400000
Nov 250000
Dec 350000
dtype: int64

Question (i) Question (ii)


First Quarter Salary April 115000
Details : dtype: int64
Jan 120000
Feb 120000
March 130000
dtype: int64
Second Quarter Salary
Details :
April 115000
May 300000
June 150000
dtype: int64
Third Quarter Salary
Details :
July 100000
Aug 250000
Sep 160000
dtype: int64
Fourth Quarter Salary
Details :
Oct 400000
Nov 250000
Dec 350000
dtype: int64

Question (iii) Question (iv)


Jan 132000.0 Jan 120000
Feb 132000.0 Feb 120000
March 143000.0 March 130000
April 126500.0 April 117400
May 330000.0 May 300000
June 165000.0 June 150000
July 110000.0 July 100000
Aug 275000.0 Aug 250000
Sep 176000.0 Sep 160000
Oct 440000.0 Oct 400000
Nov 275000.0 Nov 250000
Dec 385000.0 Dec 350000
dtype: float64 dtype: int64

Practical Question - 4

Create a Series as follows :


1 5
2 10
3 15
4 20
5 25
(i) Create a similar series but with indices 5, 4, 3, 2, 1
(ii) Remove the entry with index 5

Python Code

import pandas as pd

import numpy as np

a = [Link](5, 26, 5)

s1 = [Link](a, index = [1, 2, 3, 4, 5])

print (s1)

s2 = [Link] ([5, 4, 3, 2, 1])


print (s2)

s3 = [Link](5)

print (s3)

Output

Original Series
1 5
2 10
3 15
4 20
5 25
dtype: int64

Question (i) Question (ii)


5 25 1 5
4 20 2 10
3 15 3 15
2 10 4 20
1 5 dtype: int64
dtype: int64
Practical Question - 5

Study the following Data Frame representing quarterly sales data of 2017, 2018
and 2019 and create the same using (i) Dictionary of Series and (ii) List of
Dictionaries.

Python Code

import pandas as pd

s1 = [Link]([400000, 350000, 470000, 450000], index


= ['Qtr1', 'Qtr2', 'Qtr3', 'Qtr4'])

s2 = [Link]([420000, 370000, 490000, 470000], index


= ['Qtr1', 'Qtr2', 'Qtr3', 'Qtr4'])
s3 = [Link]([430000, 380000, 500000, 480000], index
= ['Qtr1', 'Qtr2', 'Qtr3', 'Qtr4'])

data = {2017 : s1, 2018 : s2, 2019 : s3}

df1 = [Link] (data)

print (df1)

L = [{2017 : 400000, 2018 : 420000, 2019 : 430000},


{2017 : 350000, 2018 : 370000, 2019 : 380000},
{2017 : 470000, 2018 : 490000, 2019 : 500000},
{2017 : 450000, 2018 : 470000, 2019 : 480000}]

df2 = [Link] (L, index = ['Qtr1', 'Qtr2', 'Qtr3',


'Qtr4']) #List of Dictionaries

print (df2)

Output

Question (i)
2017 2018 2019
Qtr1 400000 420000 430000
Qtr2 350000 370000 380000
Qtr3 470000 490000 500000
Qtr4 450000 470000 480000
Question (ii)
2017 2018 2019
Qtr1 400000 420000 430000
Qtr2 350000 370000 380000
Qtr3 470000 490000 500000
Qtr4 450000 Practical
470000 Question
480000 -6

Create a Data Frame showing details of employees with Name, Department,


Salary, and Bonus amount. The employee code should be the indices of the Data
Frame as shown.

Perform the following operations –


(i) Create the data frame using dictionary of lists and display the same.
(ii) Sort the datatype in alphabetical order of name and display.
(iii) Add a new column ‘Total Salary’ where ‘Total Salary = Salary + Bonus’.
Display the updated data frame.
(iv) Remove all details of E103 since he left the company. Display the modified
dataset.

Python Code
import pandas as pd

#1. Creating the DataFrame


s1 = {'Name' : ['Rohith', 'Ajay', 'Pankaj', 'Anumod'],
'Dept' : ['HR', 'Admin', 'Sales', 'Sales'],
'Salary’: [5000, 4000, 3500, 3500],
'Bonus’ : [3000, 2000, 1500, 1500]}

df1 = [Link] (s1, index = ['E101', 'E102', 'E103',


'E104']); print (df1)

#2. Sorting name in ascending order


df2 = df1.sort_values(by=['Name']); print (df2)

#3. Adding new column 'Total Salary'


df1['Total Salary'] = df1['Salary'] + df1['Bonus']
print (df1)

#4. Deleting details of E103


[Link]('E103', inplace = True); print (df1)

Output

Question (i)
Name Dept Salary Bonus
E101 Rohith HR 5000 3000
E102 Ajay Admin 4000 2000
E103 Pankaj Sales 3500 1500
E104 Anumod Sales 3500 1500

Question (ii)
Name Dept Salary Bonus
E102 Ajay Admin 4000 2000
E104 Anumod Sales 3500 1500
E103 Pankaj Sales 3500 1500
E101 Rohith HR 5000 3000
Question (iii)
Name Dept Salary Bonus Total Salary
E101 Rohith HR 5000 3000 8000
E102 Ajay Admin 4000 2000 6000
E103 Pankaj Sales 3500 1500 5000
E104 Anumod Sales 3500 1500 5000

Question (iv)
Name Dept Salary Bonus Total Salary
E101 Rohith HR 5000 3000 8000
E102 Ajay Admin 4000 2000 6000
E104 Anumod Sales 3500 1500 5000
Practical Question - 7

Create a Data Frame as shown using list of dictionaries.

Iterate over rows and columns and display results.

Python Code

import pandas as pd

data = [{'Name’: 'Aparna', 'Degree' : 'MBA', 'Score' :


90}, {'Name' : 'Pankaj', 'Degree' : 'BCA', 'Score' : 40},
{'Name' : 'Sudhir', 'Degree' : 'M. Tech', 'Score' : 80},
{'Name' : 'Geeku' , 'Degree' : 'MBA', 'Score' : 98}]

df1 = [Link] (data); print (df1)


for (x, y) in [Link]():
print ("Iterating ROWS")
print ('Row Index', x)
print ('Row Values', y)

for (a, b) in [Link]():


print ("Iterating COLUMNS")
print ('Column Name', a)
print ('Column Value', b)

Output

Iterating ROWS
Row Index 0
Row Values Name Aparna
Degree MBA
Score 90
Name: 0, dtype: object
Iterating ROWS
Row Index 1
Row Values Name Pankaj
Degree BCA
Score 40
Name: 1, dtype: object
Iterating ROWS
Row Index 2
Row Values Name Sudhir
Degree M. Tech
Score 80
Name: 2, dtype: object
Iterating ROWS
Row Index 3
Row Values Name Geeku
Degree MBA
Score 98
Name: 3, dtype: object

Iterating COLUMNS
Column Name Degree
Column Value 0 MBA
1 BCA
2 M. Tech
3 MBA
Name: Degree, dtype: object
Iterating COLUMNS
Column Name Score
Column Value 0 90
1 40
2 80
3 98
Name: Score, dtype: int64
Practical Question - 8

Write a program to iterate over a Data Frame containing names and marks,
then calculate grades as per marks (as per guidelines below) and add them to
the grade column

Python Code

import pandas as pd; import numpy as np

data = {'Name' : ['Sajeev', 'Rajeev', 'Sanjay', 'Abhay'],


'Marks': [76, 86, 55, 54], 'Grade': [[Link], [Link],
[Link], [Link]]}
print ("**** DataFrame before updation ****")

df1 = [Link] (data); print (df1)

for (x, y) in [Link]():

if y[1] >= 90:

[Link][x, 'Grade'] = 'A+'

elif y[1] >= 70 and y[1] < 90:

[Link][x, 'Grade'] = 'A'

elif y[1] >= 60 and y[1] < 70:

[Link][x, 'Grade'] = 'B'

elif y[1] >= 50 and y[1] < 60:

[Link][x, 'Grade'] = 'C'

elif y[1] >= 40 and y[1] < 50:

[Link][x, 'Grade'] = 'D'


elif y[1] < 40:

[Link][x, 'Grade'] = 'F'

print ("**** DataFrame after updation ****"); print (df1)

Output

Dataframe before updation


**** DataFrame before updation ****
Name Marks Grade
0 Sajeev 76 NaN
1 Rajeev 86 NaN
2 Sanjay 55 NaN
3 Abhay 54 NaN
Dataframe after updation
**** DataFrame after updation ****
Name Marks Grade
0 Sajeev 76 A
1 Rajeev 86 A
2 Sanjay 55 C
3 Abhay 54 C
Practical Question - 9

Create a Data Frame names ‘Cricket’ and perform all statistical functions on
the same.

Python Code

import pandas as pd

data = {'Name' : ['Sachin', 'Dhoni', 'Virat', 'Rohit',


'Shikhar'], 'Age' : [26, 25, 25, 24, 31], 'Score' : [87,
67, 89, 55, 47]}
df1 = [Link] (data); print (df1)

print ("Max score : ", df1['Score'].max())

print ("Min score : ", df1['Score'].min())

print ("Sum of score : ", df1['Score'].sum())

print ("Mean/Avg of score : ", df1['Score'].mean())

print ("Mode of score : ", df1['Score'].mode())

print ("Standard deviation of score : ",

df1['Score'].std())

print ("Variance of score : ", df1['Score'].var())

Output

Original DataFrame
Name Age Score
0 Sachin 26 87
1 Dhoni 25 67
2 Virat 25 89
3 Rohit 24 55
4 Shikhar 31 47

Max score : 89

Min score : 47

Sum of score : 345


Mean/Avg of score : 69.0

Mode of score :

0 47

1 55

2 67

3 87

4 89

dtype: int64

Standard deviation of score : 18.76166303929372

Variance of score : 352.0


Practical Question - 10

Consider the following Data Frame.

Create the above Data Frame and add the following information using
append() function.

Add job information as follows : Engr, Engr, Dr, Dr, HR, Analyst, HR

Python Code

import pandas as pd

data = {'Name' : ['Jack', 'Riti', 'Vikas', 'Neelu',


'John'], 'Age' : [34, 30, 31, 32, 16], 'City' : ['Sydney',
'Delhi', 'Mumbai', 'Banglore', 'New York'], 'Country' :
['Australia', 'India', 'India', 'India', 'US']}

df1 = [Link] (data); print (df1)

df2 = [Link]({'Name' : 'Mike', 'Age' : 17, 'City' :


'Las Vegas', 'Country' : 'US'}, ignore_index = True)

df3 = [Link]({'Name' : 'Saahil', 'Age' : 12, 'City'


: 'Mumbai', 'Country' : 'India'}, ignore_index = True)

print (df3)

df3 ['Job'] = ['Engr', 'Engr', 'Dr', 'Dr', 'HR',


'Analyst', 'HR']

print (df3)

Output

Original Data Frame


Name Age City Country
0 Jack 34 Sydney Australia
1 Riti 30 Delhi India
2 Vikas 31 Mumbai India
3 Neelu 32 Banglore India
4 John 16 New York US

DataFrame after adding records 5 and 6


Name Age City Country
0 Jack 34 Sydney Australia
1 Riti 30 Delhi India
2 Vikas 31 Mumbai India
3 Neelu 32 Banglore India
4 John 16 New York US
5 Mike 17 Las Vegas US
6 Saahil 12 Mumbai India

Adding a new column - Job


Name Age City Country Job
0 Jack 34 Sydney Australia Engr
1 Riti 30 Delhi India Engr
2 Vikas 31 Mumbai India Dr
3 Neelu 32 Banglore India Dr
4 John 16 New York US HR
5 Mike 17 Las Vegas US Analyst
6 Saahil 12 Mumbai India HR
Practical Question - 11

Consider the following Data Frame.

(i) Create the data frame


(ii) Remove all details of Alpa
(iii) Remove English and IP columns
(iv) Display Physics and Chemistry marks of Suman and Gayatri only.

Python Code

import pandas as pd

data = {'Name' : ['Suman', 'Gayatri', 'Vishruti', 'Alpa',


'Hetal'], 'English' : [74, 79, 48, 53, 68], 'Physics' :
[76, 78, 80, 76, 73], 'Chemistry' : [57, 74, 55, 89, 70],
'Biology' : [76, 85, 63, 68, 59], 'IP' : [82, 93, 69, 98,
79]}
df1 = [Link] (data); print(df1)

[Link](index = 3, inplace = True); print (df1)

[Link](columns = ['English', 'IP'], inplace = True);


print (df1)

print([Link][0 : 1, 'Name' : 'Chemistry'])

Output

Original Data Frame


Name English Physics Chemistry Biology IP
0 Suman 74 76 57 76 82
1 Gayatri 79 78 74 85 93
2 Vishruti 48 80 55 63 69
3 Alpa 53 76 89 68 98
4 Hetal 68 73 70 59 79

DataFrame after removing “Alpa”


Name English Physics Chemistry Biology IP
0 Suman 74 76 57 76 82
1 Gayatri 79 78 74 85 93
2 Vishruti 48 80 55 63 69
4 Hetal 68 73 70 59 79

Removing “English” and “IP” columns


Name Physics Chemistry Biology
0 Suman 76 57 76
1 Gayatri 78 74 85
2 Vishruti 80 55 63
4 Hetal 73 70 59
Displaying Physics and Chemistry marks of Suman and Gayatri
Name Physics Chemistry
0 Suman 76 57
1 Gayatri 78Practical74Question - 12

Consider the given Data Frame :

(i) Display Accountancy and Bst Marks of Vijay and Deepak.


(ii) Display all details of Rajat.
(iii) Display Eco marks of all Students.
(iv) Display all marks of Deepak and Ravi.

Python Code

import pandas as pd

data = {'Eco' : [89, 45, 77, 62], 'Acc' : [79, 56,


73, 42], 'BST' : [83, 39, 48, 72], 'House' : ['Mars',
'Mars', 'Saturn', 'Jupiter']}
df1 = [Link] (data, index = ['Vijay', 'Deepak',
'Ravi', 'Rajat'])

print (df1)
print ([Link]['Vijay' : 'Deepak', 'Acc' : 'BST'])

print (df1['Rajat' :])

print (df1['Eco'])

print ([Link]['Deepak' : 'Ravi', 'Eco' : 'BST'])

Output

Original Data Frame


Eco Acc BST House
Vijay 89 79 83 Mars
Deepak 45 56 39 Mars
Ravi 77 73 48 Saturn
Rajat 62 42 72 Jupiter

Acc and BST Marks of Vijay and Deepak


Acc BST
Vijay 79 83
Deepak 56 39

Details of Rajat
Eco Acc BST House
Rajat 62 42 72 Jupiter
Eco marks of all students Details of Deepak and Ravi
Vijay 89 Eco Acc BST
Deepak 45 Deepak 45 56 39
Ravi 77 Ravi 77 73 48
Rajat 62 Practical Question - 13

Create the Data Frame shown :

(i) Select rows where age is greater than 28


(ii) Select all cases where age is greater than 28 and grade is “A”
(iii) Select the degree cell where age is greater than 28 and grade is “A”
(iv) Display details of MBA and MS graduates
(v) Update Robin’s upgrade to B

Python Code

import pandas as pd; import numpy as np

data = {'first_name': ['Sam', 'Ziva', 'Kia', 'Robin',


'Kim'], 'degree': ["MBA", "MS", "Graduate", "Arts",
"MS"], 'nationality': ["USA", "India", "UK", "France",
"Canada"], 'age': [25, 29, 19, 21, 33], 'grade':['A+',
'A', 'C', [Link], 'B-']}

df1 = [Link](data, columns = ['first_name',


'degree','nationality', 'age','grade'])

print(df1)

print(df1[df1['age']>28])

print (df1[(df1['age']>28) & (df1['grade'] =='A')])

print (df1[(df1['age']>28) & (df1['grade'] ==


'A')]['degree'])

print (df1[(df1['degree'] == 'MBA') | (df1['degree'] ==


'MS')])

[Link] [3,4] = 'B'; print (df1)

Output

Original Data Frame


first_name degree nationality age grade
0 Sam MBA USA 25 A+
1 Ziva MS India 29 A
2 Kia Graduate UK 19 C
3 Robin Arts France 21 NaN
4 Kim MS Canada 33 B-

Rows with age greater than 28


first_name degree nationality age grade
1 Ziva MS India 29 A
4 Kim MS Canada 33 B-

Rows with age greater than 28 and grade is “A”


first_name degree nationality age grade
1 Ziva MS India 29 A

Details of MBA and MS graduates


first_name degree nationality age grade
0 Sam MBA USA 25 A+
1 Ziva MS India 29 A
4 Kim MS Canada 33 B-

Updation of Robin’s grade


first_name degree nationality age grade
0 Sam MBA USA 25 A+
1 Ziva MS India 29 A
2 Kia Graduate UK 19 C
3 Robin Arts France 21 B
4 Kim MS Canada 33 B-
Practical Question - 14

Create a Data Frame containing online classes information as follows :

(i) Display all details of online classes.


(ii) Display all records of False index.

Python Code

import pandas as pd

data = {'Days' : ['Sunday', 'Monday', 'Tuesday',


'Wednesday', 'Thursday'], 'Noofclasses' : [6, 0, 3, 0,
8]}

df1 = [Link] (data, index = [True, False, True,


False, True])
print (df1)

print ([Link][True]); print ([Link][False])

Output

Original DataFrame
Days Noofclasses
True Sunday 6
False Monday 0
True Tuesday 3
False Wednesday 0
True Thursday 8

Rows with True Index (Details of Online Class)


Days Noofclasses
True Sunday 6
True Tuesday 3
True Thursday 8

Rows with False Index


Days Noofclasses
False Monday 0
False Wednesday 0
Practical Question - 15

Import the following data from the CSV File “PriceList”.

Increase the price of all items by 2% and export the updated data to another
CSV File “PriceList_Updated”.

Python Code

import pandas as pd

df = pd.read_csv(r"[Link]"); print (df)


df['Price'] = df['Price'] + (df['Price']*0.02); print
(df)
df.to_csv(r"PriceList_Updated.csv")

Output

PriceList DataFrame Updated PriceList

P_ID Product_Name Price P_ID Product_Name Price


0 101 Computer 800 0 101 Computer 816.0
1 102 Laptop 1200 1 102 Laptop 1224.0
2 103 Monitor 300 2 103 Monitor 306.0
3 104 Tablet 450 3 104 Tablet 459.0
4 105 Printer 150 4 105 Printer 153.0
Practical Question - 16

Create a menu driven program to perform the following:


1. Add details and create a file “[Link]”.
2. Update details and modify csv.
3. Delete details.
4. View details.
5. Display Graph.

Python Code

import pandas as pd; import [Link] as plt

print ("**** MENU ****")

print ("1. Add Details\n2. Update Details\n3. Delete


Details\n4. View all Details\n5. Display Graph\n")

choice = int(input("Enter choice (1-5) : "))

if choice == 1:
df = [Link] (columns = ['Roll No.', 'Name',
'Marks'])

n = int(input ("Enter no. of students : "))

for i in range (n):

rn = int(input("Roll number : "))


name = input ("Enter name : ")
marks = float(input("Enter marks : "))
[Link][i] = [rn, name, marks]

print (df)

df.to_csv (r"[Link]", index = False)

elif choice == 2 :

df = pd.read_csv(r"[Link]")

rn = int(input("Roll number : "))


name = input ("Enter name : ")
marks = float(input("Enter marks : "))

index = rn-1

[Link][index, 'Name'] = name


[Link][index, 'Marks'] = marks

print (df)

df.to_csv(r"[Link]", index = False)

elif choice == 3:

df = pd.read_csv("[Link]")

rn = int(input("Roll number : "))

df1 = [Link](rn-1); print (df1)

df1.to_csv(r"[Link]")

elif choice == 4:

df = pd.read_csv(r"[Link]"); print (df)

elif choice == 5:
df1= pd.read_csv(r"[Link]")

print ("**** Your Graph ****")

x = df1['Name'].[Link]()

y = df1['Marks'].[Link]()

[Link](x, y, width = 0.5)

[Link] ("Name"); [Link] ("Marks")


[Link]("Students vs. Marks")

[Link]()

Outputs

**** MENU ****


1. Add Details
2. Update Details
3. Delete Details
4. View all Details
5. Display Graph

Choice 1 - Add Details


Enter choice (1-5) : 1

Enter no. of students : 3

Roll number : 1

Enter name : Roshan

Enter marks : 90

Roll number : 2
Enter name : Chandan

Enter marks : 97

Roll number : 3

Enter name : Bharat

Enter marks : 78
Roll No. Name Marks
0 1 Roshan 90.0
1 2 Chandan 97.0
2 3 Bharat 78.0

Choice 2 - Update Details


Enter choice (1-5) : 2

Roll number : 1

Enter name : Rahul

Enter marks : 95
Roll No. Name Marks
0 1 Rahul 95.0
1 2 Chandan 97.0
2 3 Bharat 78.0

Choice 3 - Delete Details


Enter choice (1-5) : 3

Roll number : 2
Roll No. Name Marks
0 1 Rahul 95.0
2 3 Bharat 78.0
Choice 4 - View all Details
Enter choice (1-5) : 4
Roll No. Name Marks
0 1 Rahul 95.0
1 3 Bharat 78.0

Choice 5 - Display Graph

Enter choice (1-5) : 5

**** Your Graph ****


Practical Question - 17

Consider the data given below. Using the above data, plot the following:

(i) A line chart depicting price of apps.


(ii) A bar chart depicting download of apps.
(iii) Divide the downloads value by 1000 and create a multi-bar chart depicting
price and converted download values.

The charts should have appropriate titles, legends and labels.

Python Code

Question (i)

import [Link] as plt

apps = ['Angry Birds', 'Teen Titan', 'Marvel Comics',


'ColorMe', 'Fun Run', 'Crazy Taxi']
price = [75, 120, 190, 245, 550, 55]

[Link](apps, price, 'royalblue', ls = '-.', linewidth


= 0.7, marker = '*', ms = 10, mec = 'black', mfc =
'midnightblue')

[Link] ('App Name'); [Link] ('Price')

[Link] ('Apps and its Prices')

[Link]()

Question (ii)

import [Link] as plt

apps = ['Angry Birds', 'Teen Titan', 'Marvel Comics',


'ColorMe', 'Fun Run', 'Crazy Taxi']

downloads = [197000, 209000, 414000, 196000, 272000,


311000]

[Link](apps, downloads, width = 0.5, color =


'royalblue')

[Link] ('Apps'); [Link] ('No. of downloads')


[Link] ('Apps and its number of downloads')

[Link]()

Question (iii)

import [Link] as plt; import numpy as np

Apps = ['Angry Birds', 'Teen Titan', 'Marvel Comics',


'ColorMe', 'Fun Run', 'Crazy Taxi']

p = [75, 120, 190, 245, 550, 55]

d = ([197, 209, 414, 196, 272, 311])

a = [Link](len(Apps))

[Link](a-0.2, p, color = 'b', width = 0.4, label =


'Price')

[Link](a + 0.2, d, color = 'k', width = 0.4, label =


'Downloads')

[Link] ('Apps'); [Link] ('Price & No. of


downloads')

[Link](a, Apps)
[Link] ('Apps, Prices and its number of downloads')

[Link](loc = 'upper left')

[Link]()

Outputs

Question (i) Question (ii)

Question (iii)
Practical Question - 18

Consider the data given below. Using the above data, plot the following:

i) A line chart depicting rainfall trend from Jan to July.


ii) A multibar chart representing rainfall measurement for first quarter of
year each of North, South and central region.
The charts should have appropriate titles, legends, and labels.

Python Code
Question (i)

import [Link] as plt

month = ['Jan', 'Feb', 'March', 'April', 'May', 'June',


'July']

north = [140, 130, 130, 190, 160, 200, 150]

south = [160, 200, 130, 200, 200, 170, 110]

east = [140, 180, 150, 170, 190, 140, 170]

west = [180, 150, 200, 120, 180, 140, 110]

central = [110, 160, 130, 110, 120, 170, 130]

x_axis = [Link] (len (central))

[Link] (x_axis, north, label = 'North')

[Link] (x_axis, south, label = 'South')

[Link] (x_axis, east, label = 'East')

[Link] (x_axis, west, label = 'West')


[Link] (x_axis, central, label = 'Central region')

[Link] ('Months')

[Link] ('Rainfall (in mm)')

[Link] (x_axis, month)

[Link] ('Trends in Rainfall from Jan to April')

[Link] (loc = 'upper left')

[Link]()

Question (ii)

import [Link] as plt

north = [140, 130, 130]

south = [160, 200, 130]

central = [110, 160, 130]

x_axis = [Link] (len(north))


[Link](x_axis, north, width = 0.25, label = 'North
region', color = 'springgreen')

[Link](x_axis-0.25, south, width = 0.25, label =


'South region', color = 'crimson')

[Link](x_axis+0.25, central, width = 0.25, label =


'Central region', color = 'gold')

[Link] ('Months'); [Link] ('Rainfall (in mm)')

[Link] ('Trends in Rainfall of First Quarter


(Jan/Feb/March)')

[Link](loc = 'upper left')

[Link] (x_axis, quarter)

[Link] (True)

[Link]()

Outputs

Question (i) Question (ii)


Practical Question - 19

Given the school result data. Analyze the performance of students using data
visualization techniques.

i) Draw a bar chart to represent above data with appropriate labels and title.
ii) Given subject average data for 3 years. Draw a multi bar chart to represent
the above data with appropriate labels, title, and legend.
iii) Plot a histogram for Marks data of 20 students of a class. The data (Marks
of 20 students) is as follows:
[90, 99, 95, 92, 92, 90, 85, 82, 75, 78, 83, 82, 85, 90, 92, 98, 99, 100]

Python Code

Question (i)
import [Link] as plt; import numpy as np

avg20 = [85, 88, 87, 73, 80, 90]; x_axis = [Link]


(len(avg20))

[Link](x_axis, avg20, width = 0.2, color = 'khaki')

[Link] (x_axis, ['Eng', 'Eco', 'Bst', 'Acc',


'Entre', 'Eco'])

[Link] ('Subject-wise Average Marks in 2020')

[Link] ('Subjects'); [Link] ('Average Marks')

[Link] (True)

[Link]()
Question (ii)

import [Link] as plt; import numpy as np

avg18 = [87, 88, 90, 76, 82, 90]

avg19 = [86, 87, 87, 74, 81, 91]

avg20 = [85, 88, 87, 73, 80, 90]

x_axis1 = [Link](len(avg18)) #[0, 1, 2, 3, 4, 5]

[Link](x_axis, avg18, width = 0.2, label = '2018')

[Link](x_axis+0.2, avg19, width = 0.2, label = '2019')

[Link](x_axis+0.4, avg20, width = 0.2, label = '2020')


[Link](loc = 'upper left')

[Link] = (x_axis1, ['Eng', 'Eco', 'Bst', 'Acc',


'Entre', 'Eco'])

[Link] ('Subjects Average Marks of 3 Years')

[Link] ('Subjects'); [Link] ('Average Marks')

[Link] (True)

[Link]()

Question (iii)

import [Link] as pyplot


class1 = [90, 99, 95, 92, 92, 90, 85, 82, 75, 78, 83,
82, 85, 90, 92, 98, 99, 100]

[Link](class1, bins = [75, 80, 85, 90, 95, 100],


color = "springgreen", edgecolor = "darkslategrey",
linewidth = 2)

[Link]('Marks'); [Link] ('Frequency')

[Link]()

Outputs

Question (i) Question (ii)

Question (iii)
Practical Question - 20

Export the CSV File “[Link]” and plot a bar chart with Country vs.
Total Confirmed cases.
Python Code

df = pd.read_csv ("[Link]")

country = df['Country'].tolist()
confirmedcases = df['Total Confirmed Cases']. tolist()

[Link](country, confirmedcases, width = 0.4, align =


'center', color = 'midnightblue')

[Link] ('Total Number of Covid Cases')

[Link] ('Counrty'); [Link] ('Total Cases')

[Link](rotation = 90)

[Link]()

Output

Practical Question - 21

Export the CSV File “[Link]” and plot a line chart with month
number against total profit.
Python Code

import pandas as pd; import [Link] as plt

df = pd.read_csv("[Link]"); print (df)

x = df['month_number'].tolist()

y = df['total_profit'].tolist()

[Link] (x, y, color = 'royalblue', linewidth = 1.0,


marker = '*', ms = 20, mec = 'black', mfc =
'midnightblue')

[Link] ('Month Number'); [Link]


('Total_profit')
[Link] ('Company Sales Data')

[Link]()

Output

You might also like