Ip File
Ip File
Data HanDling
PRACTICAL 01
Problem statement: Create a Series object using the Python
sequence with 5 elements.
Solution:
Source code:
import pandas as pd
L = [21, 34, 47, 58, 67]
S = pd.Series(L)
print(S)
OUTPUT:
PRACTICAL 02
Problem statement: Create a Series object using ndarray that has 5
elements in the range 60 and 120.
Solution:
Source Code:
import pandas as pd
import numpy as np
S = pd.Series(np.arange(60, 110, 10)) # Changed range
print(S)
OUTPUT:
PRACTICAL 03
Problem statement: Create a Series object using a dictionary that
stores the number of students in each section of class 12th in your
school.
Solution:
Source Code:
import pandas as pd
D = {'A': 28, 'B': 31, 'C': 33, 'D': 45, 'E': 50} # Modified number of
students
S = pd.Series(D)
print(S)
OUTPUT:
PRACTICAL 04
Problem statement: Create a Series object ‘Item’ that stores the rate
of each product as given below:
• Soap: 65
• Salt: 22
• Sugar: 45
Write code to modify the rate of soap to 55 and sugar to 50. Print the
changed rates.
Solution:
Source code:
import pandas as pd
S = pd.Series([65, 22, 45], ['soap', 'salt', 'sugar']) # Modified rates
print(S)
Jai Cricket 75
Raj Football 85
John Tennis 89
Karan Kabaddi 90
Chandu Hockey 93
Solution:
Source Code:
import pandas as pd
D = {'student': ['Jai', 'Raj', 'John', 'Karan', 'Chandu'],
'sport': ['Cricket', 'Football', 'Tennis', 'Kabaddi', 'Hockey'],
'marks': [75, 85, 89, 90, 93]} # Changed values
sport = pd.DataFrame(D, ['I', 'II', 'III', 'IV', 'V'])
print(sport)
OUTPUT:
Practical 09
Problem statement: Create a dataframe from a list containing
dictionaries of the most economical bikes with their name and rate of
three companies. The company name should be the row labels.
Solution:
Source Code:
import pandas as pd
L1 = {'Name': 'Platina', 'Cost': 70000} # Modified bike name and cost
L2 = {'Name': 'Splendor', 'Cost': 65000}
L3 = {'Name': 'CT100', 'Cost': 60000}
Bike = [L1, L2, L3]
df = pd.DataFrame(Bike, ['Bajaj', 'Hero', 'TVS'])
print(df)
OUTPUT:
Practical 10
Problem statement: Create the following dataframe sales containing year-wise sales figures for five
salespersons in INR. Use the year as column labels, and salesperson names as row labels.
Solution:
Source Code:
import pandas as pd
print("----DataFrame----")
print(sales)
print("\n----Row Labels----")
print(sales.index)
print("\n----Column Labels----")
print(sales.columns)
print(sales.tail(2))
print(sales.head(2))
OUTPUT:
.
Practical 11
Problem statement: Create a dataframe cloth as given below and write a program to:
Source Code:
import pandas as pd
cloth = pd.DataFrame(D)
print("----Dataframe----")
print(cloth)
if cloth.empty:
print("Cloth is Empty")
else:
print("\n----Transpose Dataframe----")
print(cloth.T)
print(cloth.shape)
print(cloth.count())
print(cloth.count(axis=1))
OUTPUT:
Practical 12
Problem statement: Create a dataframe cloth and perform the following operations:
Solution:
Source code:
import pandas as pd
print("----Dataframe----")
print(cloth)
print(cloth)
print(cloth)
cloth.rename(index={'C1': 'C001', 'C2': 'C002', 'C3': 'C003', 'C4': 'C004', 'C5': 'C005'}, inplace=True)
print(cloth)
cloth = cloth.drop(['C003'])
print(cloth)
del cloth['Size']
print(cloth)
OUTPUT:
Practical 13
Problem statement: Create a dataframe aid as given below and write a program to:
1. Display the books and shoes only.
2. Display toys only.
3. Display quantity in MP and CG for toys and books.
4. Display the quantity of books in AP.
Solution:
Source code:
import pandas as pd
D = {'Toys': {'MP': 7000, 'UP': 3400, 'AP': 8200, 'CG': 4500},
'Books': {'MP': 4200, 'UP': 3300, 'AP': 5600, 'CG': 2100},
'Shoes': {'MP': 5500, 'UP': 1200, 'AP': 3700, 'CG': 3200}} # Changed values
aid = pd.DataFrame(D)
print("----DataFrame----")
print(aid)
print("\n----Display the books and shoes only----")
print(aid[['Books', 'Shoes']])
print("\n----Display toys only----")
print(aid['Toys'])
print("\n----Display quantity in MP and CG for toys and books----")
print(aid.loc[['MP', 'CG'], ['Toys', 'Books']])
print("\n----Display quantity of books in AP----")
print(aid.at['AP', 'Books'])
OUTPUT:
Practical 14
Problem statement: Create a dataframe aid as given below and write
a program to save the values of aid to a comma-separated file
aidfigures.csv on the disk. Do not write the row and column labels.
Toys Books Shoes
MP 7200 4200 5500
UP 3400 3300 1200
AP 8200 5600 3700
CG 4500 2100 3200
Solution:
Source code:
import pandas as pd
D = {'Toys': {'MP': 7000, 'UP': 3400, 'AP': 8200, 'CG': 4500},
'Books': {'MP': 4200, 'UP': 3300, 'AP': 5600, 'CG': 2100},
'Shoes': {'MP': 5500, 'UP': 1200, 'AP': 3700, 'CG': 3200}} # Changed
values
aid = pd.DataFrame(D)
print(aid)
aid.to_csv('aidfigures.csv', header=False, index=False)
IMAGE:
SOLUTION:
Source Code:
PRACTICAL 26:
Problem statement: Write a SQL query to order the (student
ID, marks) table in descending order of the marks.
SOLUTION:
Source Code:
PRACTICAL 27:
Problem statement: for the given table ‘Hospital’ write SQL command
to display name all patient admitted in month of May.
SOLUTION:
Source Code:
PRACTICAL 28:
Problem statement: for the given table ‘Hospital’ write SQL
command to Display patient name in upper case with year of
admission.
SOLUTION:
Source Code:
PRACTICAL 29:
Problem statement: for the given table ‘Hospital’ Create sql query to
display first four letters of the patient name along with length of their
name who admitted before may.
SOLUTION:
Source Code:
AGGREGATE
FUNCTIONS
PRACTICALS :
PRACTICAL 30:
Insert Values:
INSERT INTO students (name, dob, gender, marks, mobile_no,
stream)
VALUES
('Alice', '2003-04-15', 'Female', 85.50, '9876543210', 'Science'),
('Bob', '2002-07-22', 'Male', 78.75, '9123456780', 'Commerce'),
('Charlie', '2003-01-10', 'Male', 92.00, '9988776655', 'Arts'),
('Daisy', '2002-12-05', 'Female', 88.25, '8765432109', 'Science'),
('Eve', '2003-05-17', 'Female', 95.00, '7654321098', 'Commerce'),
('Frank', '2002-08-30', 'Male', 70.00, '6543210987', 'Arts'),
('Grace', '2003-11-11', 'Other', 89.50, '5432109876', 'Science'),
('Hank', '2002-09-25', 'Male', 82.00, '4321098765', 'Commerce');
PRACTICAL 31:
Problem Statement: Calculate the total marks obtained by all
students in the students table.
SOLUTION:
Source Code:
PRACTICAL 32:
Problem Statement: Determine the average marks obtained by
students in the students table. This average will provide insights into
the general performance level of the student population.
SOLUTION:
Source Code:
PRACTICAL 33:
Problem Statement: Compute the average marks of
students who scored less than 90. This analysis aims to
understand the performance of students who may
need additional support or improvement.
SOLUTION:
Source Code:
PRACTICAL 34:
Problem Statement: Identify the highest marks
achieved by any student in the students table. This will
highlight the top performer and set a benchmark for
others.
SOLUTION:
PRACTICAL 35:
Problem Statement: Find the lowest marks obtained by any
student in the students table. This information is crucial for
recognizing students who may require extra assistance.
SOLUTION:
Source Code:
PRACTICAL 36:
Problem Statement: Calculate the total number of
students enrolled in the students table. This count will
provide essential demographic information for further
analysis.
SOLUTION :
Source Code:
PRACTICAL 37:
Problem Statement: Retrieve the list of students sorted
by their marks in descending order. This sorting will
facilitate quick identification of high achievers.
SOLUTION:
Source Code:
PRACTICAL 38:
Problem Statement: Generate a list of students sorted first by
their stream and then by their marks in descending order.
This will allow for a comparative analysis of performance
across different streams.
SOLUTION:
Source Code:
PRACTICAL 39:
Problem Statement: Retrieve students’ names and
their marks, sorting the results by marks using an alias
for clarity. This provides a clean and organized view of
student performance.
SOLUTION:
Source Code:
PRACTICAL 40:
Problem Statement: Calculate the average marks for each
stream in the students table. This grouping will help identify
which streams are performing well and which may need
curricular improvements.
SOLUTION:
Source Code:
PRACTICAL 41:
Problem Statement: List streams with an average mark greater
than 80. This query will highlight streams that are performing
above the average threshold and may require fewer
interventions.
SOLUTION:
Source Code:
PRACTICAL 42:
Problem Statement: Determine the number of distinct
streams present in the students table. This will provide
insights into the diversity of academic programs within the
institution.
SOLUTION:
Source Code:
BIBLIOGRAPHY:
References and Bibliography:
• Informatics Practices Class-XII NCERT Publication
• Informatics Practices Class-XII by Sumita Arora
• Think Python by Allen B Downey
• Python for everybody by Charles Severance
• Learning MYSQL by Seyed M. M. Tahaghoghi
• MySQL in a nutshell by Russell J. T. Dyer
• www.geeksforgeeks.org
• www.ncert.co.in
• www.techtipnow.in