IP Practicals
IP Practicals
1. Write python code for the following with respect to DataFrame marks, 3M
Part B
2. Write program in python to create a line chart with the given data
and also set the name to . x-axis as ename, y axis as sal, title as
simple. Also save graph as simple.pdf
ename=[‘Hari’,’Riya’,’Chandhan’,’Joe’,’Mita’]
sal=[4000,6000,5000,3500,8000] 5M
Part C
3. Write a SQL Command to do the following 7M
i. create a table called products based on the following given data.
Columns/Field Datatype Key
Pid Int Primary key
Pname Varchar(20)
Category Varchar(15)
Stock Int
Comm Decimal(6,2)
Mfgdate date
ii. insert the following data into the products table as
(1,'Phone','Electronics',35000,350.23,'2024-09-27')
(2,'Shoe','Apparel',2500,200.45,'2023-10-15')
(3,'Chair','Home',4400,40.23,'2024-11-22')
iii. Display first two characters in every product name
iv. Display product name by deleting leading/trailing spaces if there are any
v. Display the position of ‘nic’ in product category
vi. Display product names and category manufactured in February month
vii. Display all products manufactured month name
Page 1 of 8
import pandas as pd
# Creating the DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Test1': [85, 92, 78],
'Test2': [88, 94, 82],
'Test3': [90, 89, 84]
}
marks = pd.DataFrame(data)
print(marks)
print()
print(marks.head(2))
print()
marks['Total'] = marks['Test1'] + marks['Test2'] + marks['Test3']
print(marks)
Output
Name Test1 Test2 Test3
0 Alice 85 88 90
1 Bob 92 94 89
2 Charlie 78 82 84
Page 2 of 8
Name Test1 Test2 Test3 Total
0 Alice 85 88 90 263
1 Bob 92 94 89 275
2 Charlie 78 82 84 244
Page 3 of 8
1.CREATE TABLE products (
Pid INT PRIMARY KEY,
Pname VARCHAR(20),
Category VARCHAR(15),
Stock INT,
Comm DECIMAL(6,2),
Mfgdate DATE
);
2. INSERT INTO products VALUES
(1, 'Phone', 'Electronics', 35000, 350.23, '2024-09-27');
INSERT INTO products VALUES
(2, 'Shoe', 'Apparel', 2500, 200.45, '2023-10-15');
INSERT INTO products VALUES
(3, 'Chair', 'Home', 4400, 40.23, '2024-11-22');
3. SELECT SUBSTRING(Pname, 1, 2) AS FirstTwoCharacters FROM products;
4. SELECT TRIM(Pname) AS TrimmedProductName FROM products;
5. SELECT INSTR(Category, 'nic') AS PositionOfNic FROM products;
6. SELECT Pname, Category FROM products
WHERE MONTH(Mfgdate) = 2;
7. SELECT Pname, Category, DATE_FORMAT(Mfgdate, '%M') AS ManufacturedMonth
FROM products;
Output
1&2
Pid Pname Category Stock Comm Mfgdate
1 Phone Electronics 35000 350.23 2024-09-27
2 Shoe Apparel 2500 200.45 2023-10-15
3 Chair Home 4400 40.23 2024-11-22
Page 4 of 8
3. FirstTwoCharacters
Ph
Sh
Ch
4.TrimmedProductName
Phone
Shoe
Chair
5.PositionOfNic
8
0
0
6. Empty set
PART A
1. Consider the following Series Class and answer the questions.
A 45
B 36
C 27
D 35
(i) Write python code to create above series Class using list
(ii) Write a statement to delete element whose index is ‘B’
Page 5 of 8
(iii) Write a statement to add one element with 54 as value and index as ‘E’ 3M
PART B
2. Write program in python to create a bar chart that represents ATTENDANCE COUNT
of classes from 8 to 12, and also set the name to x-axis as class, y-axis as count and
title as 'Classs wise ATTENDANCE COUNT’. Also save graph as simple.pdf.
Class=[8,9,10,11,12]
ATTENDANCE COUNT=[35,30,40,45,42]
5M
PART C
3.Consider the following table Department, and write SQL commands to the following
7M
ANSWERS
1. import pandas as pd
OUTPUT
A 45
B 36
C 27
D 35
dtype: int64
A 45
C 27
D 35
dtype: int64
A 45
C 27
D 35
E 54
dtype: int64
2. Answer
import matplotlib.pyplot as plt
import numpy as np
# Data
classes = [8, 9, 10, 11, 12]
attendance_count = [35, 30, 40, 45, 42]
# Create the bar chart
result = plt.bar(classes, attendance_count, label='Attendance Count')
# Set the labels and title
plt.xlabel('Class')
plt.ylabel('Count')
plt.title('Class wise ATTENDANCE COUNT')
# Save the graph as PDF
plt.savefig('simple.pdf')
# Display the plot
plt.show()
Page 7 of 8
Output
3. Answer
Output
Page 8 of 8
3a) Consider the following Series Class and answer the questions. 3M
A 45
B 36
C 27
D 35
(i) Write python code to create above series Class using list
(ii) Write a statement to delete element whose index is ‘B’
(iii) Write a statement to add one element with 54 as value and index as ‘E’
3b) Write program in python to create a line chart with the given data and also set the name
to x-axis, y- axis and title. Also save graph as simple.pdf. 5M
ename=[‘Hari’,’Riya’,’Chandhan’,’Joe’,’Mita’]
sal=[4000,6000,5000,3500,8000]
3.c) Consider the following table and write SQL commands for all the questions 7M
Output
A 45
B 36
C 27
D 35
dtype: int64
A 45
C 27
D 35
dtype: int64
3.b Answers
import matplotlib.pyplot as plt
# Data
ename = ['Hari', 'Riya', 'Chandhan', 'Joe', 'Mita']
sal = [4000, 6000, 5000, 3500, 8000]
# Create a line chart
plt.plot(ename, sal, marker='o')
# Set names for x-axis, y-axis, and title
plt.xlabel('Employee Name')
plt.ylabel('Salary')
plt.title('Employee Salaries')
# Save the graph as simple.pdf
plt.savefig('simple.pdf')
# Show the plot
plt.show()
OUTPUT
3.C
i. CREATE DATABASE practicals;
USE practicals;
ii. CREATE TABLE Garments ( Gcode int,GarmentName VARCHAR(50), AvailableQuantity INT,
Price DECIMAL(10, 2), ReadyDate DATE);
iii.
4a) Write python code to create the following DataFrame cars using list of dictionary method.3M
4b) Write program in python to create a histogram that represents runs of batman in different
matches in the form of ranges, and also set the name to x-axis, y-axis and title. Also save
graph as simple.pdf. 5M
runs=[81,85,65,42,57,72,85,99,94,72,66,67,76]
bins=[50,60,70,80,90,100]
4c)Consider the following table and write SQL commands for all the questions 7M
Table Name: STUDENT
ROLLNO NAME DOB CITY STREAM FEE
1 Srinath 2015-11-05 Bangalore Science 2344.75
2 Supriya 2016-09-25 Nagpur Humanities 5454.50
3 Chandhan 2018-12-18 Delhi NCR Arts 1234.56
4 Giri 2012-12-12 Hyderabad Science 8746.89
5 Kaveri 2015-02-18 Chennai Science 8746.89
ANSWERS
4.a
import pandas as pd
d={'Mode':['Samurai','Accord','CR-
V','Nexon'],'Brand':['Suzuki','Honda','Honda','Tata'],'Make':[1993,1997,1997,2021]}
a=pd.DataFrame(d)
print(a)
OUTPUT
4.b
# Data
runs = [81, 85, 65, 42, 57, 72, 85, 99, 94, 72, 66, 67, 76]
bins = [50, 60, 70, 80, 90, 100]
# Create a histogram
plt.hist(runs, bins=bins, edgecolor='black')
output
4.c
output
Iv STREAM TotalStudents
Science 3
Humanities 1
Arts 1
v. NAME
Srinath
Kaveri
Vi PartialName
inat
priy
andh
ri
veri
vii. TotalFees
26527.59
5a) Write python code to create the following DataFrame emp , and write python
statements to answer the questions from (i) to (iii). 3M
5b) Write program in python to create a line chart with the given data and also set the name to
x-axis, y- axis and title. Also save graph name as simple.pdf. 5M
ename=[‘Hari’,’Riya’,’Chandhan’,’Joe’,’Mita’]
sal=[4000,6000,5000,3500,8000]
5c) Consider the following table and write SQL commands for all the questions 7M
Table Name: STUDENT
ROLLNO NAME DOB CITY STREAM FEE
1 Srinath 2015-11-05 Bangalore Science 2344.75
2 Supriya 2016-09-25 Nagpur Humanities 5454.50
3 Chandhan 2018-12-18 Delhi NCR Arts 1234.56
4 Giri 2012-12-12 Hyderabad Science 8746.89
5 Kaveri 2015-02-18 Chennai Science 8746.89
Answers
5.a
import pandas as pd
a={'Name':['Anya','Smith','Bina','Vinay','Arjun'],'Job':['Clerk','Analyst','Analyst','Clerk','Salesman'],'S
al':[500,550,480,475,540]}
b=pd.DataFrame(a)
print(b)
# (i) Add a new column called Dept
b['Dept'] = ['Maths', 'Phy', 'Chem', 'Comp', 'Phy']
print(b)
Output
5.b
ii.INSERT INTO Students (ROLLNO, NAME, DOB, CITY, STREAM, FEE) VALUES
(1, 'Srinath', '2015-11-05', 'Bangalore', 'Science', 2344.75),
(2, 'Supriya', '2016-09-25', 'Nagpur', 'Humanities', 5454.50),
(3, 'Chandhan', '2018-12-18', 'Delhi NCR', 'Arts', 1234.56),
(4, 'Giri', '2012-12-12', 'Hyderabad', 'Science', 8746.89),
(5, 'Kaveri', '2015-02-18', 'Chennai', 'Science', 8746.89);
Output
I & II
ROLLNO NAME DOB CITY STREAM FEE
1 Srinath 2015-11-05 Bangalore Science 2344.75
2 Supriya 2016-09-25 Nagpur Humanities 5454.50
3 Chandhan 2018-12-18 Delhi NCR Arts 1234.56
4 Giri 2012-12-12 Hyderabad Science 8746.89
5 Kaveri 2015-02-18 Chennai Science 8746.89
iv. Total_Science_Fee
19838.53
6.aWrite python code to create the following DataFrame marks, and write python statements
to answer the question from (i) to (iii) 3M
(i) Display first two rows.
(ii) Display last two rows.
(iii) Add a new column called Total which is sum of all Tests.
6.b) Write program in python to create a bar chart that represents pass percentage of your
school from 2020 to 2024(including both the years) and also set the name to x-axis, y-axis
and title. Also save graph as simple.pdf. 5M
years=[2020,2021,2022,2023,2024]
pass=[95,94,900,95,98]
6.c) Consider the following table and write SQL commands for all the questions 7M
Table Name: STUDENT
ROLLNO NAME DOB CITY STREAM FEE
1 Srinath 2015-11-05 Bangalore Science 2344.75
2 Supriya 2016-09-25 Nagpur Humanities 5454.50
3 Chandhan 2018-12-18 Delhi NCR Arts 1234.56
4 Giri 2012-12-12 Hyderabad Science 8746.89
5 Kaveri 2015-02-18 Chennai Science 8746.89
Answers
6a.
import pandas as pd
6a.
Test1 Test2 Test3 Test4
Ram 45 58 47 65
Peter 36 85 44 46
6.b
import matplotlib.pyplot as plt
# Data
years = [2020, 2021, 2022, 2023, 2024]
pass_percentage = [95, 94, 900, 95, 98]
Answers:
1. CREATE TABLE STUDENT ( ROLLNO INT PRIMARY KEY, NAME VARCHAR(30), DOB DATE, CITY
VARCHAR(30), STREAM VARCHAR(30), FEE DECIMAL(10,2)
2. INSERT INTO STUDENT (ROLLNO, NAME, DOB, CITY, STREAM, FEE) VALUES (1, 'Srinath', '2015-11-05',
'Bangalore', 'Science', 2344.75);
INSERT INTO STUDENT (ROLLNO, NAME, DOB, CITY, STREAM, FEE) VALUES
(2, 'Supriya', '2016-09-25', 'Nagpur', 'Humanities', 5454.50);
INSERT INTO STUDENT (ROLLNO, NAME, DOB, CITY, STREAM, FEE) VALUES (3, 'Chandhan', '2018-12-
18', 'Delhi NCR', 'Arts', 1234.56);
INSERT INTO STUDENT (ROLLNO, NAME, DOB, CITY, STREAM, FEE) VALUES (4, 'Giri', '2012-12-12',
'Hyderabad', 'Science', 8746.89);
INSERT INTO STUDENT (ROLLNO, NAME, DOB, CITY, STREAM, FEE) VALUES (5, 'Kaveri', '2015-02-18',
'Chennai', 'Science', 8746.89);
3. SELECT * FROM STUDENT ORDER BY CITY;
4. SELECT STREAM, COUNT(*) AS NumberOfStudents FROM STUDENT GROUP BY STREAM;
5. SELECT NAME, CITY FROM STUDENT WHERE MONTH(DOB) = 11;
6. SELECT SUBSTRING(NAME, 1, 2) AS FirstTwoChars FROM STUDENT;
7. DELETE FROM STUDENT WHERE CITY = 'Nagpur';
SELECT * FROM STUDENT;
I. & II
ROLLNO NAME DOB CITY STREAM FEE
1 Srinath 2015-11-05 Bangalore Science 2344.75
2 Supriya 2016-09-25 Nagpur Humanities 5454.50
3 Chandhan 2018-12-18 Delhi NCR Arts 1234.56
4 Giri 2012-12-12 Hyderabad Science 8746.89
5 Kaveri 2015-02-18 Chennai Science 8746.89
III.
ROLLNO NAME DOB CITY STREAM FEE
1 Srinath 2015-11-05 Bangalore Science 2344.75
5 Kaveri 2015-02-18 Chennai Science 8746.89
3 Chandhan 2018-12-18 Delhi NCR Arts 1234.56
4 Giri 2012-12-12 Hyderabad Science 8746.89
2 Supriya 2016-09-25 Nagpur Humanities 5454.50
IV.
STREAM NumberOfStudents
Science 3
Humanities 1
Arts 1
V.
NAME CITY
Srinath Bangalore
VI
FirstTwoChars
Sr
Su
Ch
Gi
Ka
VII.
7a) Consider the following Series GradeXII and answer the questions.
3M
A 42
B 36
C 27
D 35
(i) Write python code to create above series Class using list
(ii) Write a statement to delete element whose index is ‘B’
(iii) Write a statement to add one element with 54 as value and index as ‘E’
5M
7b) Write a Python program using Matplotlib to create a bar chart representing the
following data. Set the x-axis label to "Categories", the y-axis label to "Values", and the
title to "Simple Bar Chart". Ensure that the bar colors are set to 'skyblue' and display the
chart.
7.c) 7M
iii. Display department name and their HOD name who are working in CS department
iv. Display deptname and their deptno whose HOD is Riya
v. Display deptno from Department table
vi. Display deptno whose HOD name starts with letter ‘A’
vii. Add one more column in the name of salary
import pandas as pd
7b.
import matplotlib.pyplot as plt
# Data
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [10, 24, 36, 18]
output:
7.c
1. CREATE TABLE Department ( Deptno INT PRIMARY KEY, Deptname VARCHAR(20), HODname VARCHAR(30)
);
Output
i.ii
Deptno Deptname HODname
10 CS Riya
20 AI Shyam
30 ML Ananya
40 CL Hari
iii.
Deptname HODname
CS Riya
iv.
Deptname Deptno
CS 10
v.
Deptno
10
20
30
40
vi.
Deptno
30
vii.
Deptno Deptname HODname salary
10 CS Riya NULL
20 AI Shyam NULL
30 ML Ananya NULL
40 CL Hari NULL