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

Practical 1

Practical file

Uploaded by

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

Practical 1

Practical file

Uploaded by

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

Practicals

Data Handling
Practical 1
Problem: 1. Create a Series object using the python sequence
with 5 elements.
Solution:
Source Code:
import pandas as pd
l=[100,200,300,400,500]
s=pd.Series(l)
print(s)
Output:
Practical 2
Problem: Create s series object using ndarray that has 5 elements in
the range 100 and 150

Solution:
Source Code:
import pandas as pd
import numpy as np
s=pd.Series(np.arange(100,150,10))
print(s)
Output:
Practical 3
Problem: Create a series object using dictionary to that stores
the no of students in each section of class 12th of your school.
Solution:
Source code:
import pandas as pd
d={'A':50,'B':40,'C':46,'D':37,'E':53}
s=pd.Series(d)
print(s)
Output:
Practical 4
Problem: Create a series object ‘item’ that stores rate of each product as given below:
Bread 50
Butter 60
Sugar 39
Modify the rate of bread to 55 and sugar to 42
Solution:
Source Code:
import pandas as pd
s=pd.Series([50,60,39],['Bread','Butter','Salt’])
print(s)
s['Bread']=55
s['Sugar']=42
print("After updating values")
print(s)
Output:
Practical 5
Problem: No of students in class 11 and 12 in streams Science, Commerce, and Arts are stored in 2 series
object class 11 and 12. Write code to find total no of students in class 11 and 12.
Solution:
Source Code:
import pandas as pd
d1={'Science':44,'Commerce':38,'Arts':50}
d2={'Science':36,'Commerce':53,'Arts':27}
c11=pd.Series(d1)
c12=pd.Series(d2)
print(c11)
print(c12)
print("Total Students")
print(c11+c12)
Output:
Practical 6
Problem: Create a series ‘temp’ that stores temperature of seven days in it. Its index should be ‘Sun’, ‘Mon’,
and so on. Write a script to:
1. Display temp of first 2 days
2. Display temp of last 2 days
3. Display all temp in reverse order
4. Display temp from Tuesday to Friday
5. Display square of all temperature
Solution:
Source Code:
import pandas as pd
temp=pd.Series([38,45,35,38,41,38,44],['Sun','Mon','Tues','Wed','Thu','Fri','Sat’])
print(temp)
print("Temp of first 2 days\n",temp.head(2))
print("Temp of last 2 days\n",temp.tail(2))
print("Temp in reverse order\n",temp[::-1])
print("Temp from tuesday to friday\n",temp['Tues':'Fri’])
print("Square of all temp\n",temp*temp)
Output
Practical 7
Problem: Create a series object ‘emp’ that stores salary of 5 employees. Write a script to print:
1. Total no. of elements
2. Series id empty or not
3. Series consist NaN values or not
4. Count Non NA values
5. Axis labels
Solution:
Source code:
import pandas as pd
d={'Priya':36000,'Harsh':42000,'Sneha':38000,'Raghu':29000,'Mukul':46000}
emp=pd.Series(d)
print(emp)
print("Total no of employees",emp.size)
if emp.empty:
print("Series is empty")
else:
print("Series is not empty")
if emp.hasnans:
print("Series contains NaN elements")
else:
print("Series does not contain NaN elements")
print("Total no of non NA elements")
print("Axis labels\n",emp.axes)
Output:
Practical 8
Problem: Create the following dataframe ‘Sport’ containing sport wise marks for five students. Use 2D dictionary to
create dataframe.
Student Sport Marks
1. Jiya Tennis 88
2. Rohan Cricket 80
3. Kriti Badminton 98
4. Aryan Kabaddi 92
5. Chandu Hockey 76
Solution:
Source Code:
import pandas as pd
d={'stud':['Jiya','Rohan','Kriti','Rohan','Chandu’],
'sport':['Tennis','Cricket','Badminton','Kabaddi','Hockey’],
’marks':[88,80,98,92,76]}
sport=pd.DataFrame(d,[1,2,3,4,5])
print(sport)
Output:
Practical 9
Problem: Create a dataframe from list containing dictionaries of most economical bike with its name and
rate of three companies. Company name should be row labels.

Solution:
Source Code:
import pandas as pd
l1={'Name':'Sports','Cost':65000}
l2={'Name':'Discover','Cost':59000}
l3={'Name':'Splendor','Cost':68000}
bike=[l1,l2,l3]
df=pd.DataFrame(bike,['Hero','Motorcorp','Yamaha’])
print(df)
Output:
Practical 10
Problem: Create the following dataframe ‘sales’ containing year wise figure for five sales
person in INR. Use the year as column labels, and sales person names as row labels
2021 2022 2023 2024
Lily 2000 2500 2200 2800
Jasmine 2750 3000 2990 3200
Jack 4000 3500 3800 4500
David 1500 1800 2000 2500
Ava 5000 5600 6500 9000

Write a program to do the following:


1. Display row labels of ‘sales’
2. Display column label of ‘sales’
3. Display last 2 rows of ‘sales’
4. Display first 2 rows of ‘sales’
Solution:
Source Code:

import pandas as pd
d={2021:[2000,2750,4000,1500,5000], 2022:[2500,3000,3500,1800,5600],
2023:[2200,2990,3800,2000,6500], 2024:[2800,3200,4500,2500,9000]}
sale=pd.DataFrame(d,['Lily','Jasmine','Jack','David','Ava’])
print("----DataFrame----")
print(sale)
print("----Row label----")
print(sale.index)
print("----column abels----")
print(sale.columns)
print("----Bottom Two Rows----")
print(sale.tail(2))
print("----Top two Rows----")
print(sale.head(2))
Output:
Practical 11
Problem: Create a dataframe ‘cloth’ as given below and write program to do the following:
• Check ‘cloth’ is empty or not
• Change ‘cloth’ such that it becomes its transpose
• Display no of rows and columns of ‘cloth’
• Count and display Non NA values for each column
• Count and display Non NA values for each row

Cname Size Price


C1 Top S 1000
C2 Tshirt XL 1500
C3 Pants L 950
C4 Jeans L 1250
C5 Trouser S 600
Solution:
Source Code
import pandas as pd
d={'Cname':['Top','Tshirt','Pants','Jeans','Trousers'],'Size':['S','XL','L','L','S'],'Price':[1000,1500,950,1250,600]}
cloth=pd.DataFrame(d)
print(cloth)
print("---Checking if Dataframe is empty or not---")
if cloth.empty:
print("Cloth is empty")
else:
print("Cloth is not empty")
print("---Total no of rows and columns---")
print(cloth.shape)
print("---Transpose Dataframe---")
print(cloth.T)
print("---No of Non NA values in each column---")
print(cloth.count())
print("---No of Non NA values in each row---")
print(cloth.count(1))
Output:
Practical 12
Problem: Create a dataframe ‘cloth’ as given below and write a program to do followings:
• Change the name of Trousers to Pyjama and Jeans to Denim
• Increase price of all cloth by 10%
• Delete the data of C3 from ‘cloth’
• Delete size from cloth
Cname Size Price
C1 Top S 1000
C2 Tshirt XL 1500
C3 Pants L 950
C4 Jeans L 1250
C5 Trouser S 600
Solution:
Source Code:
import pandas as pd
d={'Cname':['Top','Tshirt','Pants','Jeans','Trousers'],'Size':['S','XL','L','L','S'],'Price':[1000,1500,950,1250,600]}
cloth=pd.DataFrame(d,['C1','C2','C3','C4','C5'])
print(cloth)
print("---Change name of Trouser to Pyjama---")
cloth.at['C4','Cname']='Pyjama'
print(cloth)
print("---Increase price of all clothes be 10%---")
cloth['Price']=cloth['Price']+cloth['Price']*10/100
print(cloth)
print("---Deleting the data of C3---")
cloth=cloth.drop(['C3'])
print(cloth)
print("---Deleting coulmn size---")
del cloth['Size']
print(cloth)
Output:
Problem: Create a dataframe ‘aid’ as given below and write program to do followings:-
Practical 13
• Display the Clothes and accessory only
• Display shoes only
• Display the quantity in MP and CG for Shoes and Clothes
• Display quantity of clothes in AP
Shoes Clothes Accessory
MP 1000 7000 1000
UP 980 6500 1500
AG 560 5500 950
CG 1500 4100 1250
Solution:
Source code:

import pandas as pd
d={'Shoes':{'MP':1000,'UP':980,'AP':560,'CG':1500},'Clothes':{'MP':7000,'UP':6500,'AP':5500,'CG':4100},'Accessory':{'MP':1000,'UP':1500,'AP':950,'CG':1250},}
aid=pd.DataFrame(d)
print(aid)
print("---Display the clothes and accessory only---")
print(aid.loc[:,['Clothes','Accessory']])
print("---Display Shoes only---")
print(aid['Shoes’])
print("---Display quantity in MP and CG for Shoes and Clothes---")
print(aid.loc[['MP','CG'],['Shoes','Clothes']])
print("---Display quantity of Clothes---")
print(aid.at['AP','Clothes'])
Output:
Practical 14
Problem: Create a dataframe ‘aid’ as given below and write program to write the values of ‘aid’ to a comma separated file
‘aidfigures.csv’ on the disk. Do not write the row labels and column labels
Shoes Clothes Accessory
MP 1000 7000 1000
UP 980 6500 1500
AG 560 5500 950
CG 1500 4100 1250
Solution:
Source Code:
import pandas as pd
d={'Shoes':{'MP':1000,'UP':980,'AP':560,'CG':1500},
'Clothes':{'MP':7000,'UP':6500,'AP':5500,'CG':4100},
‘Accessory':{'MP':1000,'UP':1500,'AP':950,'CG':1250},}
aid=pd.DataFrame(d)
print(aid)
aid.to_csv(path_or_buf='E:\\PERSONAL RELATED\\FAMILY DOCUMENTS\\SUHANI
SHARMA\\aidfigures.csv',header=False,index=False)
Output:
Practical 15
Problem: Read the data in the file ‘adfigure.csv’ into a dataframe ‘aidretrieved’ and display it. Now update the
row labels and column labels of ’aidretrieved’ to be the same as that of ‘aid’ of practical 14.
Shoes Clothes Accessory
MP 1000 7000 1000
UP 980 6500 1500
AG 560 5500 950
CG 1500 4100 1250

Solution:
Source Code:
import pandas as pd
aidretrieved=pd.read_csv('E:\\PERSONAL RELATED\\FAMILY DOCUMENTS\\SUHANI
SHARMA\\aidfigures.csv',names=['Shoes','Clothes','Accessory'],)
aidretrieved.index=['MP','UP','AP','CG’]
print(aidretrieved)
Output:
Practical 16
Problem: Collect and store total medals won by 10 countries in Olympic games and represent it in form of bar
chart with title to copare an analyze data.
Solution:
Source Code:
import matplotlib.pyplot as plt
medals=[300,250,150,99,107,234,100,28,68,200]
country=['USA','India','China','France','Brazil','South korea','Japan','Germany','Italy','UK’]
plt.bar(country,medals)
plt.title('Olympic medals’)
plt.show()
Output:
Practical 17
Problem: Speedsters Engine ltd. Is authorized dealer of different bikes companies. They record the entire
sale of bikes month wise as given below.

Jan Feb Mar Apr May Jun


Bajaj 42 61 73 85 89 69
Hero 107 97 45 101 78 122

Tvs 59 48 121 108 100 97


To get proper analysis of sale performance create multiple line chart on a common plot where all bike sale data
are plotted. Display appropriate c and y axis labels, legend and chart title
Solution:
Source Code:
import matplotlib.pyplot as plt
month=['jan','feb','mar','apr','may','jun’]
bajaj=[42,61,73,85,89,69]
hero=[107,97,45,101,78,122]
tvs=[59,48,121,108,100,97]
plt.plot(month,bajaj,label='Bajaj’)
plt.plot(month,hero,label='Hero’)
plt.plot(month,tvs,label='Tvs’)
plt.title('Speedster Enginea ltd sale analysis’)
plt.xlabel('Month’)
plt.ylabel('No of Bikes’)
plt.legend(loc='lower center’)
plt.show()
Output:
Practical 18
Problem: Given the school result data, analyses the performance of the student on different parameters. Create a dataframe for the above plot appropriate chart with title and legend.

Eng Phy Che IT Maths


9 96 76 69 97 73
10 87 88 75 90 100
11 82 94 96 89 96
12 98 78 84 100 83
Solution:
Source Code:
import pandas as pd
import matplotlib.pyplot as plt
d={'Eng':[96,87,82,98],'Phy':[76,88,94,78],'Che':[69,75,96,84],'IT':[97,90,89,100],'Math':[73,100,96,83]}
df=pd.DataFrame(d,[9,10,11,12])
print(df)
df.plot(kind='bar',title='Class wise mark analysis',xlabel='Class',ylabel='Marks')
df1=df.T
df1.plot(kind='bar',title='Subject wise marks analysis',xlabel='Class',ylabel='Marks')
plt.legend(loc='lower canter')
plt.show()
Output:
Practical 19
Problem: The following seat booking are the daily record of a month December from PVR cinemas:
125,125,136,157,129,190,201,151,159,151,201,125,144,143,131,131,171,190,201,131,143,168,181,144,144,136,157,
151,201,190,198,143
Construct a histogram from above data with 10 bin.

Solution:
Source Code:
import pandas as pd
import matplotlib.pyplot as plt
l=[125,125,136,157,129,190,201,151,159,151,201,125,144,
143,131,131,171,190,
201,131,143,168,181,144,144,136,157,
151,201,190,198,143]
plt.hist(l)
plt.title('Booking Records @ PVR')
plt.show()
Output:
MySQL
Practicals
Practical 1
Problem: Create a student table with the student id, name, and marks as attributes where the student id is
the primary key.
Solution:
Source Code:
create table Student (
-> Studid int primary key,
-> Name varchar(30),
-> Marks int
-> );
Screenshot:
Practical 2
Problem: In the table ‘Student’ created in practical 1, insert the details of Students.
Solution:
Source Code:
mysql> insert into Student values(1,'Sophia’,94);
mysql> insert into Student values(2,'Noah’,80);
mysql> insert into Student values(3,'Lily’,75);
mysql> insert into Student values(4,'Oliver’,99);
mysql> insert into Student values(5,'Emma’,63);
Screenshot:
Practical 3
Problem: Write SQL command to get the details of the Students
with marks more than 90
Solution:
Source Code:
select * from Student where marks >=90;
Practical 4
Problem: Write SQL command to find min, max, sum, and average of the marks in a
student marks table.
Solution:
Source Code:
select min(Marks) as Min_marks, max(Marks) as Max_marks, sum(Marks) as Total_marks,
avg(Marks) as Average_marks from Student;
Practical 5
Problem: Delete the details of student table create in practical 1
Solution:
Source Code:
delete from Student;
Screenshot:
Practical 6
Problem: Find the total number of customers from each country in the table (customer ID, customer
Name, country) using group by.
Solution:
Source Code:
select Country, count(Country) as 'Total_Customers' from Customer
group by Country;
Screenshot:
Practical 7
Problem: Write a SQL query to order the (student ID, Marks)table in
descending order of the marks.
Solution:
Source Code:
select * from Student order by Name DESC;
Screenshot:
Practical 8
Problem: For the given table ‘Hospital’ write SQL command to display name of all patients admitted in the
month of April.

PID PNAME ADMITDATE DEPT FEES


AP/PT/001 Ruhi Kumari 16/04/2024 Cardio 380
AP/PT/002 Lakshay Jain 04/05/2024 Neuro 300
AP/PT/003 Piyush Singh 22/04/2024 Cardio 420
AP/PT/004 Ravindra Pal 29/03/2024 ENT 220
Solution:
Source Code:
select * from Hospital where monthname(Admitdate) = 'April';
Screenshot:
Practical 9
Problem: For the given table ‘Hospital’ write SQL command to display name of patient in upper case
with year of admission.
PID PNAME ADMITDATE DEPT FEES
AP/PT/001 Ruhi Kumari 16/04/2024 Cardio 380
AP/PT/002 Lakshay Jain 04/05/2024 Neuro 300
AP/PT/003 Piyush Singh 22/04/2024 Cardio 420
AP/PT/004 Ravindra Pal 29/03/2024 ENT 220

Solution:
Source Code:
select upper(Pname) as 'Patient_Name', year(Admitdate) as 'Admit_Year' from Hospital;
Screenshot:
Practical 10
Problem: For the given table ‘Hospital’ create SQL query to display first three letters of the patient name
along with the length who admitted before May.
PID PNAME ADMITDATE DEPT FEES
AP/PT/001 Ruhi Kumari 16/04/2024 Cardio 380
AP/PT/002 Lakshay Jain 04/05/2024 Neuro 300
AP/PT/003 Piyush Singh 22/04/2024 Cardio 420
AP/PT/004 Ravindra Pal 29/03/2024 ENT 220

Solution:
Source Code:
select left(Pname,3) as 'Pname',length(Pname) as 'Length' from Hospital where month(Admitdate) < 5;
Screenshot:
Practical 11
Problem: Create a Table ‘Students’ and insert values in it.
Solution:
Source Code:
create table Students(
Rno int primary key,
Sname Varchar(20),
Gender char(1),
Marks int,
DOB date,
Stream varchar(20)
);

insert into Students values(1,'Shruti','F',83,'1999/10/21','Commerce’);


insert into Students values(2,'Kriti','F',94,'2001/07/18','Science’);
insert into Students values(3,'Shivam','M',90,'2000/11/30','Science’);
insert into Students values(4,'Jaya','F',72,'1999/02/06','Humanities’);
insert into Students values(5,'Ankush','M',65,'2000/06/19','Commerce’);
insert into Students values(6,'Raghav','M',84,'1998/11/02','Science’);
insert into Students values(7,'Misa','F',95,'2001/09/28','Science’);
insert into Students values(8,'Aakash','M',76,'1999/10/10','Humanities’);
insert into Students values(9,'Love','M',89,'1998/07/18','Commerce’);
insert into Students values(10,'Priya','F',80,'2001/04/11','Science');
Screenshot:
Practical 12
Sum() Function:
Problem 1: Display sum of all the marks in Students table.
Solution:
Source Code:
select sum(Marks) from Students;
Screenshot:

Problem 2: Display sum of all marks greater than 85 in table Students.


Solution:
Source Code:
select sum(Marks) from Students where Marks>85;
Screenshot:
Practical 13
Avg() Function:
Problem 1: Display Average of all marks in a table.
Solution:;
Source Code:
select avg(Marks) from Students;
Screenshot:

Problem 2:Display average marks of all those students who are born before 1st April, 1999.
Solution:
Source Code:
select avg(Marks) from Students where DOB < ‘1999/04/01’;
Screenshot:
Practical 14
Max() Function:
Problem: Display maximum marks from students table for males only.
Solution:
Source Code:
select max(Marks) from Students where Gender = ‘M’;
Screenshot:
Practical 15
Min() Function:
Problem: Display the minimum marks for science students only.
Solution:
Source Code:
select min(Marks) from Students where Stream = ‘Science’;
Screenshot:
Practical 16
Count() Function:
Problem 1: Display total number of records from Students table
Solution:
Source Code:
select count(*) from Students;
Screenshot:

Problem 2: Display total number of not-null marks from Students table.


Solution:
Source Code:
select count(Marks) from Students;
Screenshot:

Problem 3: Display total number of streams ignoring duplicate values from Students Table.
Solution:
Source Code:
select count(distinct Stream) from Students;
Screenshot:
Practical 17
ORDER BY Function:
Problem 1: Display Roll no, Name and Marks of Students on the basis of their Names in ascending order
Solution:
Source Code:
select Rollno, Sname, Marks from Student order by Sname;
Screenshot:

Problem 2. Display Roll no, Name, Marks of all students in descending order of their Marks and ascending order of their
names.
Solution:
Source Code:
select Rollno, Sname, Marks from Students
order by Marks desc, Sname;
Screenshot:
Problem 3: Display Roll no, Name, Marks from table Students in ascending order using alias
defined for the column Marks.
Solution:
Source Code:
select Rollno, Sname, Marks as ‘Marks_Obtained’ from Students order by
Marks_Obtained;
Screenshot:
Practical 18
GROUP BY Clause:
Problem: Display Name, Stream, and count the total number of students who have secured more than 90
marks according to their stream.
Solution:
Source Code:
select Sname, Stream, count(*) as ‘Number_of_Students’
from Students
where marks>90
group by Sname, Stream;
Screenshot:
Practical 19
HAVING Clause:
Problem: Display Stream, and sum of Marks of all students from table
Students according to their streams having maximum marks less than 85.
Solution:
Source Code:
select Stream, sum(Marks) as ‘Total_Marks’ from Students group by
Stream having max(Marks) <85;
Screenshot:

You might also like