0% found this document useful (0 votes)
52 views28 pages

IP Practical

Practical file of ip class12

Uploaded by

preeti
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)
52 views28 pages

IP Practical

Practical file of ip class12

Uploaded by

preeti
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/ 28

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

Write a program to do the following:


1. Display row labels of ‘sales’
2. Display column label of ‘sales’
3. Display last 2 rows of ‘sales’
4. Display first 2 rows of ‘sales’
Solution: Source Code:

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

Cname Size Price


C1 Top S 1000
C2 Tshirt XL 1500
C3 Pants L 950
C4 Jeans L 1250
C5 Trouser S 600
Solution: Source Code
import pandas as pd
d={'Cname':['Top','Tshirt','Pants','Jeans','Trousers'],'Size':['S','XL','L','L','S'],'Price':[1000,1500,950,1250,600]}
cloth=pd.DataFrame(d)
print(cloth)
print("---Checking if Dataframe is empty or not---")
if cloth.empty:
print("Cloth is empty")
else:
print("Cloth is not empty")
print("---Total no of rows and columns---")
print(cloth.shape)
print("---Transpose Dataframe---")
print(cloth.T)
print("---No of Non NA values in each column---")
print(cloth.count())
print("---No of Non NA values in each row---")
print(cloth.count(1))

Output:
Practical 12
Problem: Create a dataframe ‘cloth’ as given below and write a program to do followings:
• Change the name of Trousers to Pyjama and Jeans to Denim
• Increase price of all cloth by 10%
• Delete the data of C3 from ‘cloth’
• Delete size from cloth
Cname Size Price
C1 Top S 1000
C2 Tshirt XL 1500
C3 Pants L 950
C4 Jeans L 1250
C5 Trouser S 600
Solution: Source Code:

import pandas as pd
d={'Cname':['Top','Tshirt','Pants','Jeans','Trousers'],'Size':['S','XL','L','L','S'],'Price':[1000,1500,950,1250,600]}
cloth=pd.DataFrame(d,['C1','C2','C3','C4','C5'])
print(cloth)
print("---Change name of Trouser to Pyjama---")
cloth.at['C4','Cname']='Pyjama'
print(cloth)
print("---Increase price of all clothes be 10%---")
cloth['Price']=cloth['Price']+cloth['Price']*10/100
print(cloth)
print("---Deleting the data of C3---")
cloth=cloth.drop(['C3'])
print(cloth)
print("---Deleting coulmn size---")
del cloth['Size']
print(cloth)

Output:
Practical 13
Problem: Create a dataframe ‘aid’ as given below and write program to do followings:-
• Display the Clothes and accessory only
• Display shoes only
• Display the quantity in MP and CG for Shoes and Clothes
• Display quantity of clothes in AP
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)
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.

Jan Feb Mar Apr May Jun


Bajaj 42 61 73 85 89 69
Hero 107 97 45 101 78 122

Tvs 59 48 121 108 100 97


To get proper analysis of sale performance create multiple line chart on a common plot where all bike sale data
are plotted. Display appropriate c and y axis labels, legend and chart title
Solution: Source Code:
import matplotlib.pyplot as plt
month=['jan','feb','mar','apr','may','jun']
bajaj=[42,61,73,85,89,69]
hero=[107,97,45,101,78,122]
tvs=[59,48,121,108,100,97]
plt.plot(month,bajaj,label='Bajaj')
plt.plot(month,hero,label='Hero')
plt.plot(month,tvs,label='Tvs')
plt.title('Speedster Enginea ltd sale analysis')
plt.xlabel('Month')
plt.ylabel('No of Bikes')
plt.legend(loc='lower center')
plt.show()

Output:
Practical 18
Problem: Given the school result data, analyses the performance of the student on different parameters. Create a dataframe for the above plot appropriate chart
with title and legend.

Eng Phy Che IT Maths


9 96 76 69 97 73
10 87 88 75 90 100
11 82 94 96 89 96
12 98 78 84 100 83
Solution: Source Code:
import pandas as pd
import matplotlib.pyplot as plt
d={'Eng':[96,87,82,98],'Phy':[76,88,94,78],'Che':[69,75,96,84],'IT':[97,90,89,100],'Math':[73,100,96,83]}
df=pd.DataFrame(d,[9,10,11,12])
print(df)
df.plot(kind='bar',title='Class wise mark analysis',xlabel='Class',ylabel='Marks')
df1=df.T
df1.plot(kind='bar',title='Subject wise marks analysis',xlabel='Class',ylabel='Marks')
plt.legend(loc='lower canter')
plt.show()
Output:
Practical 19
Problem: The following seat booking are the daily record of a month December from PVR cinemas:
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
Construct a histogram from above data with 10 bin.
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:

You might also like