Class 12 IP Final Practical
Class 12 IP Final Practical
Practical List
Data Handling
1. Write a code to create a series object by using the Python sequence[4,6,8,10].
Code –
import pandas as pd
s = pd.Series([4,6,8,10])
print(“Series object:”)
print(s)
Output
0 4
1 6
2 8
3 10
dtype: int64
(The list of some values form the series of that values uses list index as
series index.)
Code:
import pandas as pd
lst = ['G','E','E','K','S','F',
'O','R','G','E','E','K','S']
s = pd.Series(lst)
print(s)
Output :
0 G
1 E
2 E
3 K
4 S
5 F
6 O
7 R
8 G
9 E
10 E
11 K
12 S
dtype: object
3. Create a panda's series from a dictionary of values and an ndarray.
import pandas as pd
import numpy as np
dic = {'A':22,'B':32,'C':45,'D':56}
print("Series object is created from Dictionary")
sd = pd.Series(dic)
print("Series")
print(sd)
nd = np.arange(1,10)
print("Series Object by ndarray")
sn = pd.Series(nd)
print(sn)
4. Given a series, print all the elements that are more than 50000.
import pandas as pd
def print_elements_greater_than_50000(series):
for value in series:
if value > 50000:
print(value)
s = pd.Series([10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000,
90000])
print_elements_greater_than_50000(s)
5. Create a Data Frame, quarterly sales, where each row contains the item category, item
name, and expenditure. Group the rows by the category and print the total expenditure per
category.
import pandas as pd
sales_data = { "item_category" : ["food", "drink", "food", "sweet", "food",
"sweet", "drink", "drink"], "item_name" : ["maggi", "pepsi", "rajma rice",
"gulab jamun", "chapati", "rasgulla", "coca cola", "mirinda"],
"expenditure" : [40, 68, 120, 80, 30, 160, 80, 90]}
sales_quart =pd.DataFrame(sales_data)
print(sales_quart)
gdf=sales_quart.groupby("item_category")
new_df=gdf["expenditure"].sum()
print(new_df)
import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'] > 70])
import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'].between(70,90)])
# Write a Pandas program to join the two given dataframes along rows and
assign all data.
import pandas as pd
import numpy as np
exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
exam_data1 = pd.DataFrame(exam_dic1)
exam_dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'],
'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}
exam_data2 = pd.DataFrame(exam_dic2)
1. print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2])
print(result_data)
11. Filter out rows based on different criteria such as duplicate
rows
import pandas as pd
data={'Name':['Aman','Rohit','Deepika','Aman','Deepika','Sohit','Geeta'],
'Sales':[8500,4500,9200,8500,9200,9600,8400]}
sales=pd.DataFrame(data)
# Find duplicate rows
duplicated = sales[sales.duplicated(keep=False)]
print("duplicate Row:\n",duplicated)
12. Importing and exporting data between pandas and CSV file.
# To create and open a data frame using ‘Student_result.csv’ file
using Pandas.
# To display row labels, column labels data types of each column
and the dimensions
# To display the shape (number of rows and columns) of the CSV file.
import pandas as pd
import csv
#Reading the Data
df = pd.read_csv("student_result.csv")
# Display Name of Columns
print(df.columns)
# Display no of rows and column
print(df.shape)
# Display Column Names and their types
print(df.info())
import pandas as pd
data = {'sales1':[10,20,-4,5,-1,15],
'sales2':[20,15,10,-1,12,-2]}
df = pd.DataFrame(data)
print("Data Frame")
print(df)
print('Display DataFrame after replacing every negative value with 0')
df[df<0]=0
print(df)
18. Create the following DataFrame Sales containing year wise sales figures for five
salespersonsin INR. Use the years as column labels and salesperson names as row labels.
import pandas as pd
#Creating DataFrame
d = {2018:[110, 130, 115, 118], 2019:[205, 165, 206, 198], 2020:[117, 175,
157, 183], 2021:[189, 198, 179, 169]}
sales=pd.DataFrame(d,index=['Megha','Karan','Dev','Radha'])
#Display row lables
print("Row Lables:\n",sales.index)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
#Display column lables
print("Column Lables:\n",sales.columns)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
#Display data type
print("\nDisplay column data types")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(sales.dtypes)
print("\nDisplay the dimensions, shape, size and values of Sales")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Dimensions:", sales.ndim)
print("Shape:", sales.shape)
print("Size:", sales.size)
print("Values:", sales.values)
19. Consider above dataframe in previous question and write code to do the following:
f. Display the last two rows of Sales.
g. Display the first two columns of Sales.
import pandas as pd
#Creating DataFrame
d = {2018:[110, 130, 115, 118], 2019:[205, 165, 206, 198], 2020:[117, 175,
157, 183], 2021:[189, 198, 179, 169]}
sales=pd.DataFrame(d,index=['Megha', 'Karan', 'Dev', 'Radha'])
print("Display last two rows of DataFrame:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
#Method 1
print("Using tail function:")
print(sales.tail(2))
#Method 2
print("Using iloc")
print(sales.iloc[-2:])
20. Write a panda program to read marks detail of Manasvi and calculate sum of all marks.
data = {'Manasvi': ['Physics', 'Chemistry', 'English', 'Maths', 'Computer Sc'], 'marks':
[89, 99, 97, 99, 98],}
import pandas as pd
import numpy as np
data = {'Manasvi': ['Physics', 'Chemistry', 'English','Maths', 'Computer
Sc'],
'marks': [ 89,99,97,99,98],}
df = pd.DataFrame(data )
print("Sum of Marks:")
print(df['marks'].sum())
21. Write a Pandas program to add, subtract, multiple and divide two Pandas Series.
import pandas as pd
ds1 = pd.Series([2, 4, 6, 8, 10])
ds2 = pd.Series([1, 3, 5, 7, 9])
ds = ds1 + ds2
print("Add two Series:")
print(ds)
print("Subtract two Series:")
ds = ds1 - ds2
print(ds)
print("Multiply two Series:")
ds = ds1 * ds2
print(ds)
print("Divide Series1 by Series2:")
ds = ds1 / ds2
print(ds)
import pandas as pd
s1 = pd. Series (data = [200, 100, 500, 300, 400], index= ['I', 'K', 'J',
'L', 'M'])
s2 = s1.sort_values()
print("Series object s1:")
print (s1)
print("Series object s2:")
print(s2)
23. Write a program to generate a series of 5 elements with the elements that are multiple of 7.
Start the series with the value 35. Also, multiply the index with 3.
import pandas as pd
import numpy as np
def Ser_mul7():
a = 35
n = np.arange(a,a*2,7)
s = pd.Series(index=n*3,data=n)
print(s)
Ser_mul7()
24. Write a program to generate a series of marks of 10 students. Give grace marks up to 5 of
those who are having <33 marks and print the new list of the marks.
import pandas as pd
def Ser_stumarks():
std_marks = []
for i in range(1,11):
m = int(input("Enter the marks:"))
std_marks.append(m)
s = pd.Series(index=range(1201,1211),data=std_marks)
s[s<33]=s+5
print("New List is:")
print(s[s>=33])
Ser_stumarks()
25. Write a program to generate a series of 10 numbers. Change the value of all the elements to
21 those values are multiples of 4.
import pandas as pd
numbers = []
# Generating a series
for i in range(1,11):
val = int(input("Enter a number :"))
numbers.append(val)
ser = pd.Series(numbers)
# Changing the values of multiple of four and assigning them a value 21
ser[ser % 4 == 0] = 21
print(ser)
import pandas as pd
alph =[chr(i) for i in range(65,91)]
engAlph = pd.Series(alph)
print(engAlph)
b. Vowels, having 5 elements with index labels 'a', 'e', 'i', 'o' and 'u' and all the five values
set to zero. Check if it is an empty series.
import pandas as pd
vowels = pd.Series([1,2,3,4,5],index=list('aeiou'))
print(vowels)
vowels[vowels!='not zero']=0 #
print(vowels)
print()
print('is empty',vowels.empty)
c. Friends, from a dictionary having roll numbers of five of your friends as data and their
first name as keys.
import pandas as pd
di={'data':[1,2,3,4,5],'first name':['Kips','','Python','IP','Book']}
friends = pd.Series(di)
print(friends)
import pandas as pd
MTseries = pd.Series()
print(['Series is not empty','Series is empty'][MTseries.empty])
e. MonthDays, from a numpy array having the number of days in the 12 months of a year.
The labels should be the month numbers from 1 to 12.
import pandas as pd
import numpy as np
array1 = np.array([31,28,31,30,31,30,31,31,30,31,30,31])
MonthDays = pd.Series(array1,index=[1,2,3,4,5,6,7,8,9,10,11,12])
print(MonthDays)
27. Write a program to generate a series of 10 numbers with scalar value 37.
import pandas as pd
print(pd.Series(37,range(1,11)))
28. Write a program to generate a series and print the top 5 elements using the head function.
import pandas as pd
# generating the series
ser_length = int(input("Enter the length of the series: "))
data = []
for i in range(ser_length):
val = int(input("Enter a val:"))
data.append(val)
ser = pd.Series(data)
print(ser.head(5))
29. Write a program to generate a series and print the bottom 5 elements using the tail
function.
import pandas as pd
# generating the series
ser_length = int(input("Enter the length of the series: "))
data = []
for i in range(ser_length):
val = int(input("Enter a val:"))
data.append(val)
ser = pd.Series(data)
print(ser.tail(5))
1. Given the school result data, analyses the performance of the students on different
parameters, e.g subject-wise or class-wise.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
marks = { "English" :[67,89,90,55], "Maths":[55,67,45,56],
"IP":[66,78,89,90], "Chemistry" :[45,56,67,65],
"Biology":[54,65,76,87]}
df = pd.DataFrame(marks, index=['Siya', 'Arav', 'Ishi', 'Yash'])
print("******************Marksheet****************")
print(df)
df.plot(kind='bar')
plt.xlabel(" ")
plt.ylabel(" ")
plt.show()
2. For the data frames created above, analyse and
plot appropriate charts with title and legend.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
marks = { "English" :[67,89,90,55], "Maths":[55,67,45,56],
"IP":[66,78,89,90], "Chemistry":[45,56,67,65], "Biology":[54,65,76,87]}
df = pd.DataFrame(marks, index=['Siya', 'Arav', 'Ishi', 'Yash'])
print("******************Marksheet****************")
print(df)
df.plot(kind='bar')
plt.title('Result')
plt.xlabel("marks ")
plt.ylabel("student's name ")
plt.show()
3. Take data of your interest from an open source (e.g. data.gov.in), aggregate and summarise it.
Then plot it using different plotting functions of the Matplotlib library.
CSV: census.csv
Code:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("E:\census.csv")
print(df)
df.plot(kind='bar')
plt.title('Census')
plt.xlabel("")
plt.ylabel("")
plt.show()
a) Create a bar chart to display data of Virat Kohli & Rohit Sharma.
b) Customise the chart in the following manner:
Use different widths
Use different colours to represent different years' score
Display appropriate titles for axis and chart
Show legends
c) Create a bar chart to display data of Steve Smith, Kane Williamson & Jos Butler. Customize
Chart as per your wish.
d) Display data of all players for the specific year.
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
Virat_Kohli = [2501,1855,2203,1223]
Rohit_Sharma = [1463,1985,1854,1638]
br1 = np.arange(len(Virat_Kohli))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
plt.legend()
plt.show()
Ans c)
import numpy as np
import matplotlib.pyplot as plt
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
Steve_Smith = [2340,2250,2003,1153]
Kane_Williamson = [1256,1785,1874,1974 ]
Jos_Butler = [1125,1853,1769,1436]
br1 = np.arange(len(Steve_Smith))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
plt.legend()
plt.show()
Ans d)
import numpy as np
import matplotlib.pyplot as plt
barWidth = 0.10
fig = plt.subplots(figsize =(12, 8))
Virat_Kohli = [2501,1855,2203,1223]
Steve_Smith = [2340,2250,2003,1153]
Babar_Azam = [1750,2147,1896,1008]
Rohit_Sharma = [1463,1985,1854,1638]
Kane_Williamson = [1256,1785,1874,1974 ]
Jos_Butler = [1125,1853,1769,1436]
br1 = np.arange(len(Steve_Smith))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
br4 = [x + barWidth for x in br3]
br5 = [x + barWidth for x in br4]
br6 = [x + barWidth for x in br5]
plt.legend()
plt.show()
6. The table shows passenger car fuel rates in miles per gallon for several years. Make a line graph
of the data. During which 2-year period did the fuel rate decrease?
7. The number of bed-sheets manufactured by a factory during five consecutive weeks is given
below.
8. The number of students in 7 different classes is given below. Represent this data on the bar
graph.
Class 6 7 8 9 10 11 12
Number 100 120 105 110 90 70 65
of
students
import matplotlib.pyplot as p
x=[6,7,8,9,10,11,12]
y=[130,120,135,130,150,80,75]
p.title('class strength')
p.xlabel('class')
p.ylabel('no of students')
p.bar(x, y, color = 'green', width=40)
p.show()
10.
Data Management
1. Create a student table with the student_id, name, and marks as attributes where the student_id
is the primary key.
Find the min, max, sum, and average of the marks in a student marks table.
2. Find the total number of customers from each country in the table (customer ID, customer Name,
country) using group by.
Ans. Select country, count(*) as “total customer” from customer group by country;
3. Write a SQL query to order the (student ID, marks) table in descending order of the marks
Product Table:
(i) To display the name of the entire product with price more than 100.
(ii) To display the name of all the products by the quantity more than 20