0% found this document useful (0 votes)
135 views25 pages

Practical File 2024-25

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views25 pages

Practical File 2024-25

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CLASS XII

INFORMATICS PRACTICES
PRACTICAL LIST
PYTHON
S.No. Description Date
of
submi
ssion
New Problem statement : Create a Series object using the python sequence with 5 elements:
Q1 Ans :
import pandas as pd
L = [12,23,34,45,56]
S= pd.Series(L)
print(S)
OUTPUT :

New Prob statement: 2. Create a Series object 'vowels' to store all vowels individually. Its index should be
Q2 1,2,3,4 & 5.
Ans :
import pandas as pd
vowels = pd.Series(['a','e','i','o','u'],index = [1,2,3,4,5])
print(vowels)
OUTPUT :

New Create a Pandas Series from dictionary of values and an ndarray.


Q3 Ans :
import pandas as pd
import numpy as np
#create dictionary
dict1 = {'Jan':31,'Feb':29,'Mar':31,'Apr':30,'May':31,'Jun':30,
'Jul':31,'Aug':31,'Sep':30,'Oct':31,'Nov':30,'Dec':31}

#create ndarray
ary = np.arange(2,21,2)
#create series object ser1
ser1 = pd.Series(dict1)
#create series object ser2
ser2 = pd.Series(ary)
print('Series object1:')
print(ser1)
print()
print('Series object2:')
print(ser2)
OUTPUT :

Note – Write second output “Series object2” below the first output.

New #Prob statement: Create a Series object using dictionary to that stores the number of students in each
Q4 section of class 12th of your school:
Ans :
import pandas as pd
D = {'A':23,'B':34,'C':36,'D':40,'E':32}
S = pd.Series(D)
print(S)
OUTPUT:

New Problem statement: Total no of students to be admitted is 350 in Yojna School every year.
Q5 Write code to create a Series object ‘school’ that stores these total no of students for the year 2016 to
2022:
Ans:
import pandas as pd
school = pd.Series(350,range(2016,2023))
print(school)
OUTPUT:
New Problem statement : Write a program to create a data series and then change the indexes of the Series 26 Apr
Q6 object in any random order. 2021
Ans :
import pandas as pd #import pandas library as pd
import numpy as np #import numpy library as np
data = [100,200,300,400,500]
s1 = pd.Series(data, index = ['I','J','K','L','M'])
print('Original Data Series:')
print(s1)
print()
s1 = s1.reindex(index = ['K','I','M','L','J'])
print('Data Series after changing the order of index:')
print(s1)

OUTPUT:

New Problem statement: Create a Series object ‘item’ that stores rate of each product as given 26 Apr
Q7 below: 2021
Soap 54
Salt 20
Sugar 39
Write code to modify rate of soap to 44 and sugar to 42. print the changed rate:
Ans :
import pandas as pd
item = pd.Series([54,20,39],['soap','salt','sugar'])
print(item)
item['soap']=44
item['sugar']=42
print()
print("After Updating values")
print(item)
OUTPUT:
New Problem statement: No of students in class 11 and class 12 in three streams (science, commerce and 28 Apr
Q8 humanities) are stored in 2 series object class 11 and class 12. write code to find total no of students in 2021
class 11 & class 12 stream wise:
Ans :
import pandas as pd
D1= {'Science':32,'Commerce':36,'Humanities':20}
D2= {'Science':28,'Commerce':34,'Humanities':22}
Class11 = pd.Series(D1)
Class12 = pd.Series(D2)
print(Class11)
print()
print(Class12)
print()
print('Total Students')
print(Class11 + Class12)
OUTPUT:

New Problem statement: Create a Series object ‘population’ to store population of 5 different metro
Q9 cities and display the population that are more than 300000:
Ans :
import pandas as pd
population = pd.Series([400000,25400,301100,100500,505000],
['Mumbai','Kolkata','Delhi','Chennai','Bangluru'])
print(population)
print('Population more than 300000')
print(population[population>300000])
OUTPUT :

New Problem statement: Create a series ‘temp’ that stores temperature of seven days in it. Its
Q10 index should be ‘Sunday’, ‘Monday’ ….
Write script to
1. Display temperature of first 3 days.
2. Display temperature of last 3 days.
3. Display all temperature in reverse order like Saturday, Friday,….
4. Display temperature from Tuesday to Friday.
5. Display square of all temperature.:
Ans :
import pandas as pd
temp = pd.Series([45,42,40,46,39,38,40],['Sunday','Monday','Tuesday',
'Wednesday','Thursday', 'Friday','Saturday'])
print("Temperature for seven days:")
print(temp)
print()
print("Temperature of first three days")
print(temp.head(3))
print()
print("Temperature of last three days")
print(temp.tail(3))
print()
print("Temperature in reverse order")
print(temp[::-1])
print()
print("Temperature from Tuesday to Friday")
print(temp['Tuesday':'Friday'])
print()
print("Square of all Temperature")
print(temp*temp)

OUTPUT:
New Problem statement: Create a Series object ‘employee’ that stores salary of 7
Q11
employees.Write script to print
1. Total no of elements
2. Series is empty or not
3. Series consist NaN value or not
4. Count Non-NA elements
5. Axis labels:
Ans :
import pandas as pd
D = {'ram':34000,'hari':42000,'suman':30000,'chandan':45000,
'raghu':23000}
employee = pd.Series(D)
print(employee)
print("Total no of Employees",employee.size)
if employee.empty:
print("Series is empty")
else:
print("Series is not empty")
if employee.hasnans:
print("Series contains NaN elements")
else:
print("Series does not contains NaN elements")
print("Total no of Non NA elements ",employee.count())
print("Axis labels\n", employee.axes)
OUTPUT:

New Problem statement: Create the following dataframe ‘sport’ containing sport wise marks for five
Q12
students. Use 2D dictionary having values as lists to create dataframe.

Ans :
import pandas as pd
D = {
'Student':['jai','raj','john','karan','chandu'],
'Sport':['cricket','football','tennis','kabaddi','hockey'],
'Marks':[80,76,89,92,97]
}
sport = pd.DataFrame(D,['I','II','III','IV','V'])
print(sport)
OUTPUT:

New Problem statement: Create a dataframe from list containing dictionaries of most
Q13
economical bike with its name and rate of three companies. Company name
should be the row labels.
Ans :
import pandas as pd
L1 = {'Name':'Sports','Cost':60000}
L2 = {'Name':'Discover','Cost':62000}
L3 = {'Name':'Splendor','Cost':63000}
Bike = [L1,L2,L3]
df = pd.DataFrame(Bike,['TVS','Bajaj','Hero'])
print(df)
OUTPUT:

New Write a python program to create a Dataframe from a 2D dictionary having values as dictionary
Q14 objects. Dictionary is given as following:
diSal = {
2017:{'Qtr1':2000,'Qtr2':5000,'Qtr3':10000,'Qtr4':12000},
2018:{'Qtr1':7000,'Qtr2':4000,'Qtr3':10000,'Qtr4':14000},
2019:{'Qtr1':6000,'Qtr2':9000,'Qtr3':15000,'Qtr4':12000},
2020:{'Qtr1':8000,'Qtr2':12000,'Qtr3':12000,'Qtr4':10000}
}
(i) Write code to display index labels of DataFrame
(ii) Write code to display column labels of DataFrame
(iii) Write code to find year-wise total sales.
(iv) Write code to display quarterly total sales.
Ans : import pandas as pd
diSal = {
2017:{'Qtr1':2000,'Qtr2':5000,'Qtr3':10000,'Qtr4':12000},
2018:{'Qtr1':7000,'Qtr2':4000,'Qtr3':10000,'Qtr4':14000},
2019:{'Qtr1':6000,'Qtr2':9000,'Qtr3':15000,'Qtr4':12000},
2020:{'Qtr1':8000,'Qtr2':12000,'Qtr3':12000,'Qtr4':10000}
}
#To create DataFrame 'sal_df' using dictionary 'diSal'
sal_df = pd.DataFrame(diSal)
print('DataFrame contains following data:')
print(sal_df)
print()

print("Index labels of DataFrame are:")


print(sal_df.index)
print()

print("Column labels of DataFrame are:")


print(sal_df.columns)
print()

print("Year-wise total sales:")


print(sal_df.sum(axis = 0))
print()

print("Quarterly total sales:")


print(sal_df.sum(axis = 1))
OUTPUT:
New Problem statement: Create the following dataframe ‘sales’ containing year wise
Q15
sales figurefor five sales persons in INR. Use the year as column labels, and sales
person names as row labels.
2014 2015 2016 2017
Madhu 1000 2000 2400 2800
Kusum 1500 1800 5000 6000
Kinshuk 2000 2200 7000 7000
Ankit 3000 3000 1000 8000
Shruti 4000 4500 1250 9000

Write program to do the followings


1. Display row labels of ‘sales’
2. Display column label of ‘sales’
3. Display last two rows of the ‘sales’
4. Display first two rows of the ‘sales’.
Ans :
import pandas as pd
D = {2014:[1000,1500,2000,3000,4000],
2015:[2000,1800,2200,3000,4500],
2016:[2400,5000,7000,1000,1250],
2017:[2800,6000,7000,8000,9000]}
sale = pd.DataFrame(D,['Madhu','Kusum','Kinshuk','Ankit','Shruti'])
print("----DataFrame----")
print(sale)
print()

print("----Row Labels----")
print(sale.index)
print()

print("----Column Labels----")
print(sale.columns)
print()

print("----Bottom two Rows----")


print(sale.tail(2))
print()

print("----Top two Rows----")


print(sale.head(2))
OUTPUT :
New Problem statement: Create a dataframe ‘cloth’ as given below and write
Q16
program to dofollowings:
• 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 Jeans L 1200
C2 Jeans XL 1350
C3 Shirt XL 900
C4 Trouser L 1000
C5 T-Shirt XL nan

Ans :
import pandas as pd
import numpy as np
D = { 'CName':['Jeans','Jeans','Shirt','Trouser','T-Shirt'],
'Size':['L','XL','XL','L','XL'],
'Price':[1200,1350,900,1000,np.nan]
}
cloth = pd.DataFrame(D)
print("----Dataframe----")
print(cloth)
print()

print("----checking dataframe is empty or not----")


if cloth.empty:
print("Cloth is Empty")
else:
print("Cloth is not Empty")
print()

print("----Transpose Dataframe----")
print(cloth.T)
print()

print("----Total no of rows and columns----")


print(cloth.shape)
print()

print("----No of Non NA elements in each column----")


print(cloth.count())
print()

print("----No of Non NA elements in each row----")


print(cloth.count(1))
OUTPUT:
New Problem statement: Create a dataframe ‘cloth’ as given below and write
Q17
program to dofollowings:
• Change the name of ‘Trouser’ to ‘Pant’
• Increase price of all cloth by 10%
• Rename all the indexes to [C001, C002, C003, C004, C005]
• Delete the data of C3 (C003) from the ‘cloth’
• Delete size from ‘cloth’
CName Size Price
C1 Jeans L 1200
C2 Jeans XL 1350
C3 Shirt XL 900
C4 Trouser L 1000
C5 T-Shirt XL 600

Ans :
import pandas as pd
D = { 'CName':['Jeans','Jeans','Shirt','Trouser','T-Shirt'],
'Size':['L','XL','XL','L','XL'],
'Price':[1200,1350,900,1000,600]}
cloth = pd.DataFrame(D,['C1','C2','C3','C4','C5'])
print("----Dataframe ")
print(cloth)
print()

print("----Change name of Trouser to Pant ")


cloth.at['C4','CName']='Pant'
print(cloth)
print()

print("----Increase price of all cloth by 10% ")


cloth['Price'] = cloth['Price'] + cloth['Price']*10/100
print(cloth)
print()

print("----Rename all the indexes to [C001,C002,C003,C004,C005] ")


cloth.rename(index = {'C1':'C001','C2':'C002','C3':'C003','C4':'C004','C5':'C005'},inplace = 'True')
print(cloth)
print()

print("----Delete the data of C003 ")


cloth = cloth.drop(['C003'])
print(cloth)
print()

print("----Delete column 'Size' ")


del cloth['Size']
print(cloth)

OUTPUT:
New Problem statement: Create a dataframe ‘aid’ as given below and write
Q18
program to do followings:
1. Display the books and shoes only
2. Display toys only
3. Display quantity in MP and CG for toys and books.
4. Display quantity of books in AP
5. Draw the bar chart for State names and quantity of Toys.
Toys Books Shoes
MP 7000 4300 6000
UP 3400 3200 1200
AP 7800 5600 3280
CG 4100 2000 3000
Ans :
import pandas as pd
D = {'Toys':{'MP':7000,'UP':3400,'AP':7800,'CG':4100},
'Books':{'MP':4300,'UP':3200,'AP':5600,'CG':2000},
'Shoes':{'MP':6000,'UP':1200,'AP':3280,'CG':3000},}
aid = pd.DataFrame(D)
print('----DataFrame ')
print(aid)
print()

print('----Display the books and shoes only ')


print(aid.loc[:,['Books','Shoes']])
print()

print('----Display toys only ')


print(aid['Toys'])
print()

print('----Display quantity in MP and CG for toys and books ')


print(aid.loc[['MP','CG'],['Toys','Books']])
print()

print('----Display quantity of books in AP ')


print(aid.at['AP','Books'])
print()

x = aid.index
y = aid['Toys']
print(x)
print(y)
plt.bar(x,y)
plt.xlabel('State Name')
plt.ylabel('Toys Quantity')
plt.title('State Names and Quantity')
plt.show()
OUTPUT:
New Given a data frame namely data as shown in adjacent figure (fruit names are row labels). Write code
Q19 statement to :
(a) Write code to create dataframe ‘data’ as show in figure. Also print dataframe ‘data’.
(b)Find all rows with label “Apple”. Extract all columns.
(c) List fruits with count more than 25.
(d) List single True or False to signify if all prices are
more than 100 or not.
(e) List 2nd , 3rd and 4th rows.
(f) Draw bar chart for Fruit and their price.
Ans :
#(a)
import pandas as pd
import matplotlib.pyplot as plt
#list object 'lst1' created
lst1 = [['Red',3,120],
['Green',9,110],
['Red',25,125],
['Green',26,150],
['Green',99,70]]
#DataFrame named 'data' created
data = pd.DataFrame(lst1, columns = ['Color','Count','Price'],
index = ['Apple','Apple','Pear','Pear','Lime'])

print("DataFrame contain following Data:")


print(data)
print()

#(b)
print(data.loc['Apple',:])
print()

#(c)
print(data.loc[data.Count>25])
#OR
print(data[data['Count']>25])
print()

#(d)
print((data['Price']>100).all())
#OR
print((data.Price>100).all())
print()

#(e)
print(data.iloc[1:4,:])
#or
print(data.iloc[[1,2,3],:])

#(f)
x = ['Apple Red','Apple Green','Pear Red','Pera Green','Lime']
plt.bar(x,data['Price'])
plt.xlabel('Fruit Name')
plt.ylabel('Price')
plt.title('Fruit Price Chart')
plt.show()

OUTPUT:
New Given a data frame namely df as shown in adjacent figure . Write code statement to :
Q20 (a) Write code to create the dataframe ‘df’ as shown in
figure. Also print dataframe ‘df’.
(b) List only the columns rollno and marks using loc.
(c) List only columns 0 and 2 (columns indexes) using iloc.
(d) List only rows with labels ‘rank1’ and ‘rank4’ using loc.
(e) List only rows, row index 0,2,3 using iloc.
(f) Write code to draw Bar chart to display student’s marks with name.
Ans :
#a
import pandas as pd
lst = [[101,'amit',99], [102,'vikas',92],
[103,'ajay',85], [104,'riya',83]]
df = pd.DataFrame(lst, columns = ['rollno','name','marks'],
index = ['rank1','rank2','rank3','rank4'])
print("DataFrame 'df' contain following data:")
print(df)
print()

#b
print(df.loc[:,['rollno','marks']])
print()

#c
print(df.iloc[:,[0,2]])
print()

#d
print(df.loc[['rank1','rank4'],:])
print()

#e
print(df.iloc[[0,2,3],:])

#f
x = df['name']
y = df['marks']
plt.bar(x,y)
plt.xlabel('Name')
plt.ylabel('Marks')
plt.title('Marks obtained')
plt.show()
New Draw a Bar chart from school result of class 12-A, 12-B data that analyse the performance of the students
Q21 pass percentage subject wise. Data is as given:
subject = [ 'Physics', 'Chemistry', 'Maths', 'IP', 'English']
pass_per = [85, 78, 65, 100, 90] .
Ans :
import matplotlib.pyplot as plt
subject = ['Physics','Chemistry','Maths','IP','English']
pass_per = [85,78,65,100,90]
plt.bar(subject,pass_per,color = 'b', width = .5)
plt.xlabel('Subject Name')
plt.ylabel('Pass Percentage')
plt.title('School Result - Pass Percentage Subject wise')
plt.show()
New Write a program to plot a bar chart from the medal won by top four countries. Make sure that bars are
Q22 separately visible.
Country Gold Silver Bronze Total
Australia 80 59 59 198
England 45 45 46 136
India 26 20 20 66
Canada 15 40 27 82
Ans :
import matplotlib.pyplot as plt
import numpy as np
Info = ['Gold','Silver','Bronze','Total']
Australia = [80,59,59,198]
England = [45,45,46,136]
India = [26,20,20,66]
Canada = [15,40,27,82]
X = np.arange(len(Info))
plt.bar(Info,Australia,width = .15, label='Australia')
plt.bar(X + .15,England, width = .15, label = 'England')
plt.bar(X + .30, India, width = .15, label = 'India')
plt.bar(X + .45, Canada, width = .15, label = 'Canada')
plt.legend()
plt.xlabel('Medal Type')
plt.ylabel('Medal Won')
plt.title('Top four countries tally')
plt.show()
20 Consider the following table FITNESS with details about fitness product being sold in the store. Write
command of SQL for (i) to (x)
Table : FTNESS
Pcode Pname Price Manufacturer
P1 Treadmill 21000 Coscore
P2 Bike 20000 Aone
P3 Cross Trainer 14000 Reliable
P4 Multi Gym 34000 Coscore
P5 Massage Chair 5500 Regrosene
P6 Belly Vibrator Belt 6500 Ambawya

Ans :
(i) To display the names of all the products with price more than 20000.
Ans :

(ii) To display the names of all products by the manufacturer “Aone”.


Ans:

(iii)To count distinct manufacturer from fitness.


Ans :

(iv) To find minimum price from fitness.


Ans:

(v) To find average price for manufacturer “Coscore”.


Ans :

(vi) To display the names of all the products whose name starts with “M”.
Ans:
(vii) To display Pcode and Pname of all Products , whose Manufacturer is Reliable or Coscore.
Ans:

(viii) To change the Products name to “Fit Trend India” of the Product , whose Pcode as “P6”.
Ans:

(ix) To change the price data of all the products by applying 25% discount reduction.
Ans :

(x) Display details of all the products with unit price in the range 20000 to 30000.

21 Write SQL commands for the following on the basis of given table CLUB
Table: CLUB
Coach_id Coachname Age Sports Dateofapp Pay Gender
1 Kukreja 35 Karate 27/03/1996 1000 M
2 Ravina 34 Karate 20/01/1998 1200 F
3 Karan 34 Squash 19/02/1998 2000 M
4 Tarun 33 Basketball 01/01/1998 1500 M
5 Zubin 36 Swimming 12/01/1998 750 M
6 Ketaki 36 Swimming 24/02/1998 800 F

(1) To show all information about swimming coaches in the club.


Ans:
(2) To list names of all coaches with their data of appointment (Dateofapp) in descending order.
Ans:

(3) To display a report showing coachname, pay, age and bonus (15% of pay) for all coaches.
Ans:

(4) Write query to count distinct sports from club.


Ans:

(5) To find minimum age of female coach.


Ans :

(6) To display average pay for sports is “Karate’


Ans:

(7) To display total pay for coaches whose data of appointment is after ‘31/01/1998’
Ans:

(8)To display name and pay of coaches after arranging the record on the basis of pay in ascending order.
Ans:

(9) Count the number of coaches gender-wise form table club.


Ans:

(10) Display the sum of pay sports – wise.


Ans:

You might also like