Class 12 IP File 23 24
Class 12 IP File 23 24
Visualization
Given the school result data, analyze the
12. performance of the students on different parameters,
e.g., subject wise or class wise.
Write a program to plot the graph for result analysis
13. based on different subjects using the Data frame with
title and legends.
Take data of weights of 24 people and analyze
14. the data using the histogram.
Data
Management
Create a student table with the student id, name,
15. and marks as attributes where the student id is the
primary key.
16. Insert the details of a new student in the above table.
Source Code:
#import pandas as pd
import pandas as pd
# import numpy as np
import numpy as np
# numpy array
arr = np.array(['A','B','K','S','F', 'O','E','K','S'])
# forming series
Sr = pd.Series(arr)
# output
print("Series from Array", Sr)
# simple dict
dict = {'A':82,'B':40,'K':8,'J':92, 'L':19,'Z':15,'N':16}
# forming series
s = pd.Series(dict)
# output
print("Series from Dictionary",s)
Output:
Source Code:
Output:
Source Code:
import pandas as pd
import numpy as np
a=np.array([1,3,4,7,8,8,9])
Sr=pd.Series(a)
print("Series is\n",Sr)
s=df.quantile([.75])
print("Result is \n",s)
Output:
Series is
0 1
1 3
2 4
3 7
4 8
5 8
6 9
dtype: int32
Result is
0.75 8.0
dtype: float64
Program 4: Write a program to create a series and display its first 5 and last 5
elements.
Source Code:
import pandas as pd
import numpy as np
Sr=pd.Series([1,3,4,7,8,8,9])
print("First five elements of series:\n",Sr.head(5))
print("Last five elements of series:\n",Sr.tail(5))
Output:
Source Code:
import pandas as pd
data={'Category':['Medical','Transport','Grocery','Clothes'],'Item
Name':['Sanitizer','Cab','Snacks','Summer Wears'],'Expenditure':[1000,4250,1500,5200]}
quarterly_expenditure=pd.DataFrame(data)
print(quarterly_expenditure)
print(quarterly_expenditure['Expenditure'].sum())
Output:
import pandas as pd
Output:
Index:
RangeIndex(start=0, stop=4, step=1)
Columns:
Index(['Name', 'Math', 'Science', 'English'], dtype='object')
Data type:
Name object
Math int64
Science int64
English int64
dtype: object
Shape:
(4, 4)
Program 7: Write a program to create a dataframe and display its first 2 and last
2 elements.
Source Code:
import pandas as pd
results = {'Name': ['Ilma', 'Astha', 'Mohini', 'Anju'],'Math': [87, 92, 94, 90],'Science': [89, 93, 95,
87],'English': [78, 85, 89, 80]}
Output:
Source Code:
import pandas as pd
import numpy as np
exam_data = {'name': ['Muskan', 'Nidhi', 'Kanishka', 'Jiya', 'Anjali', 'Manu', 'Mehak', 'Leena', 'Kavya',
'Joya'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19], 'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
print(df)
Output:
Source Code:
import pandas as pd
import numpy as np
exam_data = {'name': ['Muskan', 'Nidhi', 'Kanishka', 'Jiya', 'Anjali', 'Manu', 'Mehak', 'Leena', 'Kavya',
'Joya'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],'qualify':
['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
#adding the column Roll_No
df["Roll_No"]=[1,2,3,4,5,6,7,8,9,10]
print("Data Frame after adding column Roll_No\n", df)
#Deleting the row
df=df.drop('e',axis=0)
print("\nData Frame after adding column Roll_No\n",df)
Output:
Data Frame after adding column Roll_No
name score attempts qualify Roll_No
a Muskan 12.5 1 yes 1
b Nidhi 9.0 3 no 2
c Kanishka 16.5 2 yes 3
d Jiya NaN 3 no 4
e Anjali 9.0 2 no 5
f Manu 20.0 3 yes 6
g Mehak 14.5 1 yes 7
h Leena NaN 1 no 8
i Kavya 8.0 2 no 9
j Joya 19.0 1 yes 10
Source Code:
import pandas as pd
import numpy as np
exam_data = {'name': ['Muskan', 'Nidhi', 'Kanishka', 'Jiya', 'Anjali', 'Muskan', 'Mehak', 'Leena', 'Kavya',
'Joya'],
'score': [12.5, 9, 16.5, np.nan, 9, 12.5, 14.5, np.nan, 8, 19],'attempts': [1, 3, 2, 3, 2, 1, 1, 1, 2, 1],'qualify':
['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
print("Data Frame\n", df)
#Filtering the duplicate rows
df=df[df.duplicated(keep=False)]
print("\n Duplicate rows\n",df)
Output:
Data Frame
name score attempts qualify
a Muskan 12.5 1 yes
b Nidhi 9.0 3 no
c Kanishka 16.5 2 yes
d Jiya NaN 3 no
e Anjali 9.0 2 no
f Muskan 12.5 1 yes
g Mehak 14.5 1 yes
h Leena NaN 1 no
i Kavya 8.0 2 no
j Joya 19.0 1 yes
Duplicate rows
name score attempts qualify
a Muskan 12.5 1 yes
f Muskan 12.5 1 yes
Program 11: Importing and exporting data between pandas and CSV file.
Source Code:
import pandas as pd
import numpy as np
exam_data = {'name': ['Muskan', 'Nidhi', 'Kanishka', 'Jiya', 'Anjali', 'Manu', 'Mehak', 'Leena', 'Kavya',
'Joya'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
print(df1)
Output:
name score attempts qualify
b Nidhi 9.0 3 no
d Jiya NaN 3 no
e Anjali 9.0 2 no
h Leena NaN 1 no
i Kavya 8.0 2 no
[ ]:
Program 12: Given the school result data for the last five years, analyze
the results on different parameters, e.g., subject wise or class wise.
Source Code:
import pandas as pd
import matplotlib.pyplot as plt
years=[2020,2021,2022,2023,2024]
class_X=[89,92,75,86,98]
class_XII=[74,87,75,90,92]
classes=[class_X,class_XII]
plt.plot(years, class_X,label='class_X')
plt.plot(years,class_XII,label='class_XII')
plt.xlabel('Years')
plt.ylabel('Marks')
plt.legend(loc='upper left')
plt.title("Result Analysis")
plt.show()
Output:
Program 13: Write a program to plot the graph for result analysis based on
different subjects using the Data frame with title and legends.
Source Code:
import pandas as pd
import matplotlib.pyplot as plt
student={"name":['Ilma', 'Astha', 'Mohini', 'Anju'],'Math':[87, 92, 94, 90],'Science': [89, 93, 95,
87],'English': [78, 85, 89, 80]}
df=pd.DataFrame(student)
df.plot(kind="bar",x="name")
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title("Result Analysis")
plt.show()
Output:
Program 14: Take data of weights of 24 people and analyze the data
using the histogram.
Source Code:
import pandas as pd
import matplotlib.pyplot as plt
weight=[20,21,22,23,35,36,41,45,49,47,52,56,55,58,59,65,62,68,71,75,81,95,98,100]
plt.hist(weight,bins=8)
plt.ylabel('Weight')
plt.title("Weight Analysis")
plt.show()
Output:
Program 15: Create a student table with the student id, name, and marks
as attributes where the student id is the primary key.
SQL Command:
Output:
Program 16: Insert the details of a new student in the above table.
SQL Command:
Output:
Program 17: Delete the details of students who are having ‘i’ as the last
character in his/her name from the above table.
SQL Command:
Output:
Program 18: Use the select command to get the details of the
students with marks more than 70.
SQL Command:
Output:
Program 19: Find the min, max, sum, and average of the marks in the student
table.
SQL Command:
Output:
Program 20: Find the total number of customers from each country in
the table (customer ID, customer Name, country) using group by.
Customer table:
SQL Command:
Output:
Program 21: Write a SQL query to order the (student ID, marks) table in
descending order of the marks.
SQL Command:
Output:
Program 22: Write the SQL query to find out day , day name, month name
and year of the date (29/02/2000).
SQL Command:
Output:
DAY("2020-02-29")
29
MONTH("2020-02-29")
2
YEAR("2020-02-29")
2020
DAYNAME("2020-02-29")
Saturday
MONTHNAME("2020-02-29")
February
Program 23: Write the SQL query to find Mod, round and power of a
number.
SQL Command:
Output:
Program-24: Write the SQL query to execute various string functions.
SQL Command:
Output: