TH
REENA 12 B
INFORMATICS
PRACTICE
PRACTICAL FILE
PRACTICALS
DATA HANDLING
Practical 1
Problem statement :1 Create a series object using the python sequence with 5 elements:
Solution :
Source code:
import pandas as pd
list1 = [2,4,6,8,10]
series1= pd .Series (list1)
print(series1)
Screenshot:
Output
Practical 2
Problem statement: 1. Create a 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
s2 = pd. Series (np.arange(100,150,8))
print(s2)
Screenshot output
Practical 3
Problem statement: 1. 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
d1 = {'A':36,'B':37,'C':60,'E':45,'F':67}
s1 = pd . Series (d1)
print(s1)
Screenshot:
Output
Practical 4
Problem statement :1 Create a series object ‘item’ that stores rate of each product as given below
:
Soap 10
Sugar 20
Apple 30
Write code to modify rate of soap to 44 and sugar to 42 ,print the changed rate:
Solution :
Source code:
import pandas as pd
s1 = pd . Series ([10,20,30],['soap','sugar','apple'])
print(s1)
s1['soap'] = 44
s1['sugar'] = 42
print ('after updating the values')
print(s1)
Screenshot Output
Practical 5
Problem statement :No of students in class 11 and class 12 in three streams (science ,commerce and arts) are
stored in 2 series object class 11 and class 12.Write code to find total no of students in class 11 and class 12 stream
wise:
Solution :
Source code:
import pandas as pd
d1 = {'science':55,'commerce':56,'arts':59}
d2= {'science':45,'commerce':37,'arts':66}
class11 = pd.Series(d1)
class12 = pd.Series(d2)
print (class11)
print(class12)
print ('total number of students ')
print(class11 + class12)
Screenshot:
Output
Practical 6
Problem statement : Create a series ‘temp’ that stores temperature of seven days in it. Its index
should be ‘Sunday’, ‘Monday’ ….
Write script to
1. Display temp of first 3 days.
2. Display temp of last 3 days.
3. Display all temp in reverse order like Saturday, Friday,….
4. Display temp from Tuesday to Friday.
5. Display square of all temperature
Solution :
Source code:
import pandas as pd
temp = pd.Series([44,42,40,38,36,38,40], index =
['Sunday','Monday','Tuesday','wednesday','Thursday','Friday','Saturday']
)
print(temp)
print("Temp of first three days\n",temp.head(3))
print("Temp of last three days\n",temp.tail(3))
print("Temp in reverse order\n", temp[::-1])
print("Temp from Tuesday to Friday\n",temp['Tuesday':'Friday'])
print("Square of all Temprature\n",temp*temp)
Screenshot:
Output
Practical 7
Problem statement : Create a Series object ‘employee’ that stores salary of 7 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 :
Solution :
Source code:
import pandas as pd
d1 = {'radha':34000,'heena':42000,'suhani':30000,'pooja':45000,'chandu':23000}
employee = pd.Series(d1)
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)
Screenshot:
Output:
Practical 8
Problem statement : Create the following dataframe ‘Sport’ containing sport wise marks for five
students. Use 2D dictionary to create dataframe.
Student Sport Marks
1. Jaya Cricket 80
2. Reena Football 76
3. Suhani Tennis 89
4. Pooja Kabaddi 92
5. Gauri Hockey 97
:
Solution :
Source code:
import pandas as pd
d1 = {'student':['jaya','reena','suhani','pooja','gauri'],
'sport':['cricket','football','tennis','kabaddi','hockey'],
'marks':[80,76,89,92,97]}
sport = pd.DataFrame(d1, [1,2,3,4,5])
print(sport)
Screenshot:
Output
Practical 9
Problem statement : Create a dataframe from list containing dictionaries of most economical bike
with its name and rate of three companies. Company name should be the row labels
Solution :
Source code:
import pandas as pd
list1 = {'Name':'Sports','Cost':80000}
list2 = {'Name':'bullet','Cost':90000}
list3 = {'Name':'splendor','Cost':45000}
Bike = [list1,list2,list3]
df = pd.DataFrame(Bike, ['TVS','Royal_Enfield','Hero'])
print(df)
Screenshot:
Output
Practical 10
Problem statement : : Create the following dataframe ‘sales’ containing year wise sales figure for
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
➢ Khushi 1500 1800 5000 6000
➢ Khushbu 2000 2200 7000 7000
➢ Ankita 3000 3000 1000 8000
➢ Suhani 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
Solution :
Source code:
import pandas as pd
dict1 = {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(dict1,['Madhu','Khushi','Khushbu','Ankita','Suhani'])
print("----DataFrame----")
print(sale)
print("----Row Labels----")
print(sale.index)
print("----Column Labels----")
print(sale.columns)
print("----Bottom two Rows----")
print(sale.tail(2))
print("----Top two Rows----")
print(sale.head(2))
Screenshot : output
Practical 11
Problem statement : : : Create a dataframe ‘cloth’ as given below and write program to do
followings:
• 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 Pant XL 1000
C3 Shirt XL 800
C4 Trouser L 600
C5 Coat XL 400
Solution :
Source code:
import pandas as pd
dict1 = {'CName':['Jeans','Pant','Shirt','Trouser','Coat'],
'Size':['L','XL','XL','L','XL'],
'Price':[1200,1000,800,600,400]}
cloth = pd.DataFrame(dict1)
print("----Dataframe----")
print(cloth)
print("----checking dataframe is empty or not----")
if cloth.empty:
print("Cloth is Empty")
else:
print("Cloth is not Empty")
print("----Transpose Dataframe----")
print(cloth.T)
print("----Total no of rows and
columns----")
print(cloth.shape)
print("----No of Non NA elements in
each column----")
print(cloth.count())
print("----No of Non NA elements in
each row----")
print(cloth.count(1))
Screenshot
Practical 12
Problem statement : Create a dataframe ‘cloth’ as given below and write program to do
followings:
• Change the name of ‘Trouser’ to ‘pant’ and Jeans to ‘Denim’
• 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 Pant XL 1000
C3 Shirt XL 800
C4 Trouser L 600
C5 Coat XL 400
Solution :
Source code:
import pandas as pd
dict1 = {'CName':['Jeans','Pant','Shirt','Trouser','Coat'],
'Size':['L','XL','XL','L','XL'],
'Price':[1200,1000,800,600,400]}
cloth = pd.DataFrame(dict1, index =['C1','C2','C3','C4','C5'])
print("----Dataframe----")
print(cloth)
print("----Change name of Trouser to Pant----")
cloth.at['C4','CName']='Pant'
#cloth.CName['C4']='Pant'
#cloth.loc['C4','CName']='Pant'
print(cloth)
print("----Increase price of all cloth by 10%----")
cloth['Price'] = cloth['Price'] + cloth['Price']*10/100
print(cloth)
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("----Delete the data of C003----")
cloth = cloth.drop(['C003'])
print(cloth)
print("----Delete column 'Size'----")
del cloth['Size']
print(cloth)
Screenshot:
Output
Practical 13
Problem statement : t: Create a dataframe ‘aid’ as given below and write 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
Toys Books Shoes
MP 200 1000 1800
UP 400 1200 2000
AP 600 1400 2200
CG 800 1600 2400
Solution :
Source code:
import pandas as pd
dict1 = {'Toys':{'MP':200,'UP':400,'AP':600,'CG':800},
'Books':{'MP':1000,'UP':1200,'AP':1400,'CG':1600},
'Shoes':{'MP':1800,'UP':2000,'AP':2200,'CG':2400}}
aid = pd.DataFrame(dict1)
print('----DataFrame----')
print(aid)
print('----Display the books and shoes only----')
print(aid.loc[:,['Books','Shoes']])
print('----Display toys only----')
print(aid['Toys'])
print('----Display quantity in MP and CG for toys and books----')
print(aid.loc[['MP','CG'],['Toys','Books']])
print('----Display quantity of books in AP----')
print(aid.at['AP','Books'])
Screenshot: Output:
Practical 14
Problem statement : 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.
Toys Books Shoes
MP 200 1000 1800
UP 400 1200 2000
AP 600 1400 2200
CG 800 1600 2400
Solution :
Source code:
import pandas as pd
dict1 = {'Toys':{'MP':200,'UP':400,'AP':600,'CG':800},
'Books':{'MP':1000,'UP':1200,'AP':1400,'CG':1600},
'Shoes':{'MP':1800,'UP':2000,'AP':2200,'CG':2400}}
aid = pd.DataFrame(dict1)
print('----DataFrame----')
print(aid)
aid.to_csv(path_or_buf = 'C:/Users/ReenaGauri/OneDrive/Desktop/Documents/aidfigures.csv',
header = False, index = False)
Screenshot: Output:
Practical 15
Problem statement : Read the data in the file ‘aidfigure.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.
Toys Books Shoes
MP 200 1000 1800
UP 400 1200 2000
AP 600 1400 2200
CG 800 1600 2400
Solution :
Source code: import pandas as pd
aidretrieved = pd.read_csv('C:/Users/ReenaGauri/OneDrive/Desktop/Documents/aidfigures.csv',
names=['Toys','Books','Shoes'],)
aidretrieved.index = ['MP','UP','AP','CG']
print(aidretrieved)
Screenshot: Output:
Practical 16
Problem statement : : Collect and store total medals won by 10 countries in Olympic games and
represent it in form of bar chart with title to compare an analyze data.
Solution :
Source code: import matplotlib.pyplot as plt
medals = [200,210,220,230,280,290,300,400,500,489]
country = ['China','Egypt','Japan','Italy','Russia',
'Korea','Chile','Austria','India','UK']
plt.bar(country, medals)
plt.title(' Total Olympics Medals of ten countries')
plt.show()
Screenshot: Output:
Practical 17
Problem statement : Techtipnow Automobiles is authorized dealer of different Bikes companies.
They record the entire sale of bikes month wise as give below:
Jan Feb Mar April May June
Royal
Enfield 300 400 500 600 700 800
Yamaha
Motors 10 20 30 40 50 60
Suzuki
Motors 15 30 45 60 75 90
To get proper analysis of sale performance create multiple line chart on a common plot where all bike
sale data are plotted. Display appropriate x and y axis labels, legend and chart title.
Solution :
Source code:
import matplotlib.pyplot as plt
month = ['jan','feb','mar','april','may', 'june']
royalenfield = [300,400,500,600,700,800]
yamahamotors = [10,20,30,40,50,60]
suzukimotors = [15,30,45,60,75,90]
plt.plot(month,royalenfield, label = 'royal_enfield')
plt.plot(month,yamahamotors, label = 'yamaha_motors')
plt.plot(month,suzukimotors, label = 'suzuki_motors')
plt.title('Techtipnow Automobiles Sale Analysis')
plt.xlabel('Month')
plt.ylabel('No of Bikes')
plt.legend( loc = 'upper center')
plt.show()
Screenshot:
Output
Practical 18
Problem statement : : Given the school result data, analyses the performance of the student on
different parameters, e.g. subject wise or class wise. Create a dataframe for the above, plot
appropriate chart with title and legend.
15 70 60 60 100 70
16 89 91 84 90 98
17 90 50 45 82 20
18 92 82 90 50 100
Solution :
Source code:
import pandas as pd
import matplotlib.pyplot as plt
dict1 = {'eng':[70,89,90,92],'math':[60,91,50,82],
'phy':[60,84,45,90],'chm':[100,90,82,50],
'IT':[70,98,20,100]}
df = pd.DataFrame(dict1,[15,16,17,18])
print(df)
df.plot(kind = 'bar',title = 'Class Wise Marks Analysis',
xlabel = 'Class', ylabel = 'Marks')
df1 = df.T
df1.plot(kind = 'bar',title = 'Subject Wise Marks Analysis',
xlabel = 'Class', ylabel = 'Marks')
plt.legend(loc = 'upper left')
plt.show()
Screenshot: Output
Practical 19
Problem statement :: : The following seat bookings are the daily records of a month December
from PVR cinemas
(110,108,124,198,178,155,145,195,167,138,
159,188,173,121,148,154,154,165,156,183,133,196)
Construct a histogram from above data with 10 bin.
Solution :
Source code:
import pandas as pd
import matplotlib.pyplot as plt
list1= [110,108,124,198,178,155,145,195,167,138,
159,188,173,121,148,154,154,165,156,183,133,196]
plt.hist(list1)
plt.title("Booking Records @ PVR")
plt.show()
Screenshot: Output