Final Ip Practical File
Final Ip Practical File
Practical No.2
2.Write a program to create a Series object using a dictionary that stores the
number of students in each section of class 12 in your school?
Solution:
import pandas as pd
students = {'A':39,'B':41,'C':42,'D':44}
s1 =
pd.Series(students)
print(s1)
Output
1
Practical No.3
3.Consider the series object S that stores the contribution of each section, as
shown below:
A 4300
B 6500
C 3900
D 6100
Write code to modify the amount of section ‘A’ as 3400 and for section ‘C’ and ‘D’ as
5000. Print the changed object.
Solution:
import pandas as pd
D = {'A':4300,'B':6500,'C':3900,'D':6100}
S=
pd.Series(D)
S[0] = 3400
S[2:] = 5000
print(S)
Output
2
Practical No.4
4.Write a program to create a Data Frame Quarterly Sales where each row
contains the Quarterly Sales of TV, Freeze and AC. Show the DataFrame after
deleting details of Freeze.
Solution:
import pandas as
pd PData = {
'TV' : {'QTR1' : 200000, 'QTR2': 230000, 'QTR3' : 210000, 'QTR4':24000},
'Freeze' : {'QTR1' : 300000, 'QTR2': 200000, 'QTR3' : 290000, 'QTR4':210000},
'AC' : {'QTR1' : 240000, 'QTR2': 153000, 'QTR3' : 245000, 'QTR4':170000}
}
df =
pd.DataFrame(PData)
print(df)
del df['Freeze']
print(df)
Output
3
Practical No.5
5.Write a program to create a DataFrame to store Roll Number, Name and Marks
of five students. Print the DataFrame and its transpose.
Solution:
import pandas as
pd PData = {
'Roll Number' : [101,102,103,104,105],
'Name' : ['Karan','Taran','Piyush','Bhupinder','Simran'],
'Marks' : [82,85,79,86,94]
}
df = pd.DataFrame(PData)
print("Original
DataFrame") print(df)
print("DataFrame after
Transpose") print(df.T)
Output
4
Practical No.6
6.Write a program to create a DataFrame from a CSV file. Display the shape
(Number of rows and columns) of the CSV file.
Solution:
import pandas as pd
df = pd.read_csv('F://PP.csv') # All Columns
print(df)
rows, columns = df.shape
print("Number of rows : ",rows)
print("Number of columns :
",columns)
Output
Practical No.7
7.Write a program to create a DataFrame which contains RollNumber, Name,
Height and Weight of 05 students. Create a CSV file using this DataFrame.
Solution:
import pandas as
pd PData = {
'Roll Number' : [101,102,103,104,105],
'Name' : ['Karan','Taran','Piyush','Bhupinder','Simran'],
'Height' : [5.4,5.9,4.8,4.11,5.1],
'Weight' : [54,52,49,46,53]
}
df =
pd.DataFrame(PData)
print("DataFrame")
print(df)
5
df.to_csv('dataStudents.csv',sep='\t',index=False,header=['Roll Number','Name
of Student','Height','Weight'],columns=['Roll Number','Name','Height','Weight'])
print("Data stored in csv file")
Output
6
Practical No.8
8.Given the school result data, analyses the performance of the students in
Accountancy and Business studies using bar graph.
Solution:
import matplotlib.pyplot as
plt import numpy as np
label = ['Anil', 'Vikas', 'Dharma', 'Mahen', 'Manish', 'Rajesh']
Accountancy = [94,85,45,25,50,54]
BusinessStudy =[98,76,34,65,55,88]
index = np.arange(len(label))
plt.bar(index, Accountancy,label="Accountancy", color='g')
plt.bar(index+0.3, BusinessStudy,label = "Business Study", color='r')
plt.xlabel('Student Name', fontsize=12)
plt.ylabel('Percentage', fontsize=12)
plt.xticks(index, label, fontsize=12,
rotation=90)
plt.title('Percentage of Marks achieve by student Class XII')
plt.legend()
plt.show()
Output
Practical No.9
7
9.Given datasets population of India and Pakistan in the given years. Show the
population of India and Pakistan using line chart with title and labels of both axis.
Year 1960 1970 1980 1990 2000 2010
Population of Pakistan 44.91 58.09 78.07 107.7 138.5 170.6
Population of India 449.48 553.57 696.783 870.133 1000.4 1309.1
Solution:
from matplotlib import pyplot as plt
year = [1960, 1970, 1980, 1990, 2000, 2010]
pop_pakistan = [44.91, 58.09, 78.07, 107.7, 138.5, 170.6]
pop_india = [449.48, 553.57, 696.783, 870.133, 1000.4, 1309.1]
plt.plot(year, pop_pakistan, color='k')
plt.plot(year, pop_india,
color='orange') plt.xlabel('Countries')
plt.ylabel('Population in million')
plt.title('Pakistan India Population till 2010')
plt.show()import matplotlib.pyplot as plt
plt.show()
Output
8
Practical No.10
10. Write a program to plot a bar chart from the medals won by four
countries. Make sure that bars are separately visible.
Solution:
from matplotlib import pyplot as
plt import numpy as np
plt.figure(figsize = (10,7))
info =
['Gold','Silver','Bronze','Total'] India
= [80,59,59,198]
England = [45,45,46,136]
Australia = [26,20,20,66]
Canada = [15,40,27,82]
X = np.arange(len(info))
plt.bar(info,India,width = 0.15,label="India")
plt.bar(X + 0.15,England,width = 0.15,label="England")
plt.bar(X + 0.30,Australia,width = 0.15,label="Australia")
plt.bar(X + 0.45,Canada,width = 0.15,label = "Canada")
plt.xlabel('Medal Type')
plt.ylabel('Medals')
plt.title('Medals won by four
countries') plt.legend()
plt.show()
Output
9
Practical No.11
11. A survey gathers height and weight of 60 participants and recorded
the participants’ ages as:
Ages = [1,1,2,3,5,7,8,9,10,10,11,13,13,15,16,17,18,19,20,21,21,23,24,24,24,25,25,
25,25,26,26,26,27,27,27,27,27,29,30,30,30,30,31,33,34,34,34,35,36,36,37,37,37,3
8,38,39,40,40,41,41]
Write a program to plot a histogram from above data with 20 bins.
Solution:
from matplotlib import pyplot as plt
Ages = [1,1,2,3,5,7,8,9,10,10,11,13,13,15,16,17,18,19,20,
21,21,23,24,24,24,25,25, 25,25,26,26,26,27,27,27,
27,27,29,30,30,30,30,31,33,34,34,34,35,36,36,37,
37,37,38,38,39,40,40,41,41]
plt.hist(Ages,bins = 20)
plt.title("Participants' Ages
Histogram") plt.show()
Output
10
Practical No.12
12. Write a program to create data series and then change the indexes of the
Series object in any random order.
Solution:
import pandas as pd
import numpy as np
s1 = pd.Series(data = [100,200,300,400,500], index = ['I','J','K','L','M'])
print("Original Data Series") print(s1)
s1 = s1.reindex(index = ['K','L','M','J','I'])
print("Data Series after changing the index")
print(s1)
Output
Practical No.13
13. In an online contest, two 2-player teams’ point in 4 rounds are stored in two
DataFrames as shown below:
Team 1’s points (df1) Team 1’s points (df2)
P1 P2 P1 P2
1 700 490 1 1100 1400
2 975 460 2 1275 1260
3 970 570 3 1270 1500
4 900 590 4 1400 1190
11
Write a program to calculate total points earned by both the teams in each round.
Solution:
import pandas as pd
data1 = {'P1' : {'1':700,'2':975,'3':970,'4':900},
'P2' : {'1':490,'2':460,'3':570,'4':590}}
data2 = {'P1' : {'1':1100,'2':1275,'3':1270,'4':1400},
'P2' : {'1':1400,'2':1260,'3':1500,'4':1190}}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
print("Performace of Team - 1")
print(df1)
print("Performace of Team - 2")
print(df2)
print("Points earned by both Teams")
print(df1 + df2)
Output
12
Practical No.14
14. Write a program to create a DataFrame with two columns name and age.
Add a new column Updated Age that contains age increased by 10 years.
Solution:
import pandas as pd
data = {'Name' : ['Amit','Sumit','Rahul','Dev'],
'Age' : [20,22,16,15]
}
df = pd.DataFrame(data)
print("Original DataFrame")
print(df)
df['Updated Age'] = df['Age'] + 10
print("DataFrame After adding column Updated Age")
print(df)
Output
13
Practical No.15
15. Given a DataFrame ( df ) as under:
City Schools
Delhi 7654
Mumbai 8700
Kolkata 9800
Chennai 8600
Write a program to print DataFrame one row at a time. Also display first two rows of City
column and last two rows of Schools column.
Solution:
import pandas as pd
data = {'City' : ['Delhi','Mumbai','Kolkata','Chennai'],
'Schools' : [7654,8700,9800,8600]
}
df = pd.DataFrame(data)
for i, j in df.iterrows():
print(j)
print(" ")
print("*******************************")
print("First two rows of City Column")
print(df.City.head(2))
print("*******************************")
print("Last two rows of Schools Column")
print(df.Schools.tail(2))
14
Output
Practical No.16
15
16. Write a program to plot a horizontal bar chart from heights of some stdents.
Solution:
import matplotlib.pyplot as plt
import numpy as np
Names = ['Ram','Shyam','Rama','Kashish','Mayank']
Heights = [5.1,5.5,6.0,5.0,6.3]
Weights = [20,30,15,18,22]
X = np.arange(len(Names))
plt.barh(Names,Heights, height = 0.5,label = "Height", color ='g')
plt.barh(X+0.5,Weights, height =0.5,label = "Weight", color ='b')
plt.xlabel("Height")
plt.ylabel("Names")
plt.legend()
plt.show()
Output
16
Practical No.17
17. Create a data frame for examination result and display row labels, column labels
data types of each column and the dimensions.
Practical No.18
17
18. Replace all negative values in a data frame with a 0.
Practical No.19
18
19. Replace all missing values in a data frame with a 999.
Practical No.20
19
20. Importing and exporting data between pandas and CSV file.
Practical No.21
21.Importing and exporting data between pandas and MySQL database.
20
Exporting data from Data Frame to MYSQL
21
22
Practical No.22
3) Display Product Name and Price from table Product where product name starts
with “B”.
FROM PRODUCT
Practical No.23
23
23. Consider the following tables HOSPITAL. Write SQL commands for (i) to (iv)
and give outputs for SQL queries (v) to (viii).
SELECT *
FROM Hospital
WHERE length(name) = 6;
b. To reduce Rs. 200 from the charge of female patients who are in
Cardiology department.
UPDATE Hospital
SET Charge = Charge – 200
WHERE Sex = ‘F’ AND Department = ‘Cardiology’;
c. To insert a new row in the above table with the following data :
d. To remove the rows from the above table where age of the patient > 60.
Sum(Charge)
1200
COUNT(DISTINCT Department )
4
Department Sum(Charge)
Cardiology 1300
ENT 700
Orthopedic 1150
Surgery 1050
h. Select Name from HOSPITAL where Sex=’F’ AND Age > 20;
Name
Zayana
Dhanya
Practical No.24
25
24. Create a student table with the student id, name, and marks as attributes
where the student id is the primary key.
Practical No.25
Practical No.26
Practical No.27
27. Use the select command to get the details of the students with
marks more than 80.
26
Practical No.28
29. Create a foreign key in one of the two tables mentioned above
28. Create a new table (order ID, customer Name, and order Date) by
joining two tables (order ID, customer ID, and order Date) and (customer ID,
customer
Practical No.29
Practical No.30
30. Find the min, max, sum, and average of the marks in a student marks
table. Practical No.31
31. Find the total number of customers from each country in the table
(customer ID, customer Name, country) using group by.
27
28
Practical No.32
32. Create a new table (name, date of birth) by joining two tables (student
id, name) and (student id, date of birth).
Practical No.33
33. Write a SQL query to order the (student ID, marks) table in descending
order of the marks.
29