Practical 1
Practical 1
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
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
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.
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.
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)
);
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 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: