Class 12 Informatics Practices Practical file 2024-2025
1.Create a Series with following lists:
section=[‘A’,’B’,’C’,’D’,’E’] #INDEXES
strength=[40,38,33,35,30] #DATA
import pandas as pd import numpy as np section=['A','B','C','D','E'] strength=[40,38,33,35,30] s=pd.Series(index=section,data=strength) print(s)
Output
A 40 B 38 C 33 D 35 E 30 dtype: int64
2. Create a Series by using following dictionary:
Department={‘Computer’:30,’Finance’:20,’HR’:5}
strength=[40,38,33,35,30] #DATA
import pandas as pd import numpy as np Department={'Computer':30,'Finance':20,'HR':5} s=pd.Series(Department) print(s)
Output
Computer 30 Finance 20 HR 5 dtype: int64
3. Display those values from series which are more than 80.
import pandas as pd import numpy as np Data=[89,34,45,90,33,45,67,91] s=pd.Series(Data) print(s[s>80])
Output
0 89 3 90 7 91 dtype: int64
4. Create a DataFrame by using following dictionary:
Items={ ‘Havels’:{‘Fan’:100,’LED’:20,’Sockets’:50,’Switches’:45},
‘Goldmetal’:{‘Fan’:150,’LED’:30,’Sockets’:40,’Switches’:50},
‘Sunrise’:{‘Fan’:80,’LED’:25,’Sockets’:35,’Switches’:55} }
import pandas as pd import numpy as np Items={ 'Havels':{'Fan':100,'LED':20,'Sockets':50,'Switches':45}, 'Goldmetal':{'Fan':150,'LED':30,'Sockets':40,'Switches':50}, 'Sunrise':{'Fan':80,'LED':25,'Sockets':35,'Switches':55} } d=pd.DataFrame(Items) print('Details of Items') print(d)
Output
Details of Items Havels Goldmetal Sunrise Fan 100 150 80 LED 20 30 25 Sockets 50 40 35 Switches 45 50 55
5. Create a dataframe using given dictionary and find number of records column wise as well as row wise.
Doctors={ ‘Fever’:{‘Amit’:20,’Rahul’:30,’Radhika’:15,’Naina’:50},
‘Cold’:{‘Amit’:10,’Rahul’:13,’Radhika’:5,’Naina’:52},
‘Typhoid’:{‘Amit’:12,’Rahul’:10,’Radhika’:35,’Naina’:np.NAN}}
import pandas as pd import numpy as np Doctors={ 'Fever':{'Amit':20,'Rahul':30,'Radhika':15,'Naina':50},\ 'Cold':{'Amit':10,'Rahul':13,'Radhika':5,'Naina':52},\ 'Typhoid':{'Amit':12,'Rahul':10,'Radhika':35,'Naina':np.NAN}} d=pd.DataFrame(Doctors) print(d) print('Number of records (columns wise)') print(d.count()) print('Number of records (row wise)') print(d.count(axis=1))
Output
Fever Cold Typhoid Amit 20 10 12.0 Rahul 30 13 10.0 Radhika 15 5 35.0 Naina 50 52 NaN Number of records (columns wise) Fever 4 Cold 4 Typhoid 3 dtype: int64 Number of records (row wise) Amit 3 Rahul 3 Radhika 3 Naina 2 dtype: int64
6. Create dataframe by using given dictionary and find sum of quantity row wise as well as column wise.
Items={ ‘Havels’:{‘Fan’:100,’LED’:20,’Sockets’:50,’Switches’:45},
‘Goldmetal’:{‘Fan’:150,’LED’:30,’Sockets’:40,’Switches’:50},
‘Sunrise’:{‘Fan’:80,’LED’:25,’Sockets’:35,’Switches’:55} }
import pandas as pd import numpy as np Items={ 'Havels':{'Fan':100,'LED':20,'Sockets':50,'Switches':45}, 'Goldmetal':{'Fan':150,'LED':30,'Sockets':40,'Switches':50}, 'Sunrise':{'Fan':80,'LED':25,'Sockets':35,'Switches':55} } print("=====================") print('Sum of quantity Brand wise') print(d.sum()) print("=====================") print('Sum of quantity - Item wise') print(d.sum(axis=1))
Output
Sum of quantity Brand wise Havels 215 Goldmetal 270 Sunrise 195 dtype: int64 ===================== Sum of quantity - Item wise Fan 330 LED 75 Sockets 125 Switches 150 dtype: int64
7. Create a DataFrame by using following dictionary and display marks of ‘Sunita’ only:
Students={
‘Ramesh’:{‘Eng’:80,’Maths’:66,’Hindi’:78,’AI’:90},
‘Sunita’:{‘Eng’:74,’Maths’:86,’Hindi’:82,’AI’:92},
‘Naresh’:{‘Eng’:56,’Maths’:93,’Hindi’:88,’AI’:99},
}
import pandas as pd import numpy as np Students={ 'Ramesh':{'Eng':80,'Maths':66,'Hindi':78,'AI':90}, 'Sunita':{'Eng':74,'Maths':86,'Hindi':82,'AI':92}, 'Naresh':{'Eng':56,'Maths':93,'Hindi':88,'AI':99}, } d=pd.DataFrame(Students) print("Marks of Sunita") for i,row in d.iterrows(): print(i,row['Sunita'])
Output
Marks of Sunita Eng 74 Maths 86 Hindi 82 AI 92
8. Create a DataFrame by using following dictionary and display average marks in all subjects:
Students={
‘Ramesh’:{‘Eng’:80,’Maths’:66,’Hindi’:78,’AI’:90},
‘Sunita’:{‘Eng’:74,’Maths’:86,’Hindi’:82,’AI’:92},
‘Naresh’:{‘Eng’:56,’Maths’:93,’Hindi’:88,’AI’:99},
}
import pandas as pd import numpy as np Students={ 'Ramesh':{'Eng':80,'Maths':66,'Hindi':78,'AI':90}, 'Sunita':{'Eng':74,'Maths':86,'Hindi':82,'AI':92}, 'Naresh':{'Eng':56,'Maths':93,'Hindi':88,'AI':99}, } d=pd.DataFrame(Students) print("Subject wise average marks") print(d.mean(axis=1))
Output
Eng 70.000000 Maths 81.666667 Hindi 82.666667 AI 93.666667 dtype: float64
9. Create a DataFrame by using following dictionary and display degree wise maximum marks:
Data={
‘name’:[‘Ram’,’Sham’,’Seeta’,’Harish’,’Anoop’,’Sahil’],
‘degree’:[‘BCA’,’BCA’,’BCA’,’MCA’,’MBA’,’BCA’],
‘marks’:[87,56,90,89,57,88]
}
import pandas as pd import numpy as np Data={ 'name':['Ram','Sham','Seeta','Harish','Anoop','Sahil'], 'degree':['BCA','BCA','BCA','MCA','MBA','BCA'], 'marks':[87,56,90,89,57,88] } d=pd.DataFrame(Data) print(d.groupby('degree')['marks'].max())
Output
BCA 90 MBA 57 MCA 89 Name: marks, dtype: int64
10. Create a DataFrame by using following dictionary and display number of students who have opted ‘BCA’:
Data={
‘name’:[‘Ram’,’Sham’,’Seeta’,’Harish’,’Anoop’,’Sahil’],
‘degree’:[‘BCA’,’BCA’,’BCA’,’MCA’,’MBA’,’BCA’],
‘marks’:[87,56,90,89,57,88]
}
import pandas as pd import numpy as np Data={ 'name':['Ram','Sham','Seeta','Harish','Anoop','Sahil'], 'degree':['BCA','BCA','BCA','MCA','MBA','BCA'], 'marks':[87,56,90,89,57,88] } print('Number of students in BCA:') print(d.groupby('degree')['marks'].count()['BCA'])
Output
Number of students in BCA: 4
11. Replace all negative values in dataframe with -1.
import pandas as pd s1={ 'Data1':[2,3,-5,4,-9], 'Data2':[-4,7,-2,3,5] } df=pd.DataFrame(s1) print(df) print('After replacing negative values with -1') df[df<0]=-1 print(df)
Output
Data1 Data2 0 2 -4 1 3 7 2 -5 -2 3 4 3 4 -9 5 After replacing negative values with -1 Data1 Data2 0 2 -1 1 3 7 2 -1 -1 3 4 3 4 -1 5
12. Replace all missing values in dataframe with 0.
import pandas as pd s1={ 'Data1':[2,3,np.NAN,4,np.NAN], 'Data2':[-4,7,-2,3,np.NAN] } df=pd.DataFrame(s1) print(df) print('After replacing missing values with 0') df=df.fillna(0) print(df)
Output
Data1 Data2 0 2.0 -4.0 1 3.0 7.0 2 NaN -2.0 3 4.0 3.0 4 NaN NaN After replacing missing values with 0 Data1 Data2 0 2.0 -4.0 1 3.0 7.0 2 0.0 -2.0 3 4.0 3.0 4 0.0 0.0
13. Save dataframe data into a csv file named hospital.csv
import pandas as pd import numpy as np Doctors={ 'Fever':{'Amit':20,'Rahul':30,'Radhika':15,'Naina':50},\ 'Cold':{'Amit':10,'Rahul':13,'Radhika':5,'Naina':52},\ 'Typhoid':{'Amit':12,'Rahul':10,'Radhika':35,'Naina':np.NAN}} df=pd.DataFrame(Doctors) df.to_csv("hospital.csv") print("File created")
Output
File Created
14. Reading data from csv file named hospital.csv into dataframe and display it.
import pandas as pd df=pd.read_csv("hospital.csv") print('Content of file:') print(df)
Output
File Created
15. Python program to save data from a datagram to MYSQL table marks.
import pandas as pd import mysql.connector as sqltor mycon=sqltor.connect(host="localhost",user="root",passwd="root",database="data") cur=mycon.cursor() Marks={ 'rollno':[1,2,3,4,5,6], 'name':['Aman','Suman','Raman','Ayush','Ramesh','Rajan'], 'Percentage':[67,74.7,98.2,56.6,98.1,89] } d=pd.DataFrame(Marks) for (r,rowdata) in d.iterrows(): r=rowdata[0] n=rowdata[1] m=rowdata[2] cur.execute("insert into marks values(%d,%s,%f);"%(r,n,m,)) mycon.commit() mycon.close() print("Record Saved")
Output
Record Saved
16. Python program to read and display records from MYSQL table student.
import pandas as pd import mysql.connector as sqltor mycon=sqltor.connect(host="localhost",user="root",passwd="root",database="data") if mycon.is_connected(): df=pd.read_sql("Select * from student",mycon) print(df) else: print("Connection not established") mycon.close()
Output
rollno name marks 0 1 aman 98.0 1 2 Raman 85.0 2 3 Suman 58.0 3 4 Manu 95.0 4 5 Raju 94.0
17. Python program to read and display records from MYSQL table student where marks are more than 90.
import pandas as pd import mysql.connector as sqltor mycon=sqltor.connect(host="localhost",user="root",passwd="root",database="data") if mycon.is_connected(): df=pd.read_sql("Select * from student where marks>90",mycon) print(df) else: print("Connection not established") mycon.close()
Output
rollno name marks 0 1 aman 98.0 1 4 Manu 95.0 2 5 Raju 94.0
18. Python program to display bar graph on the basis of two lists Months=[‘Jan’,’Feb’,’Mar’,’Apr’]
and Attendance=[87,68,90,78]
import matplotlib.pyplot as plt Months=['Jan','Feb','Mar','Apr'] Attendance=[87,68,90,78] plt.bar(Months,Attendance,align="center",color="green") plt.show()
Output
19. Python program to display pie chart on the basis of two lists:
State=[‘Punjab’,’Jharkhand’,’Rajasthan’,’UP’]
Density=[72,62,45,78]
import matplotlib.pyplot as plt State=['Punjab','Jharkhand','Rajasthan','UP'] Density=[72,62,45,78] plt.pie(Density,labels=State) plt.show()
Output
20. Write SQL statement to create a table named student with following structure.
Field Name Data Type Size rollno int 3 Name Varchar 20 Marks Float Class Varchar 10
Answer
Create Table Student(rollno int(3), Name Varchar(20), Marks Float, Class Varchar(10));
21. Write SQL statements to insert following records in the above table.
1 Amandeep 5000 XII
2 Raman 4500 XII
3 Joel 3500 X
4 Radhika 4800 XI
5 Suman 4200 XI
Answer
Insert into Student values(1, 'Amandeep',5000,'XII'); Insert into Student values(2, 'Raman',4500,'XII'); Insert into Student values(3, 'Joel',3000,'X'); Insert into Student values(4, 'Radhika',4800,'XI'); Insert into Student values(5, 'Suman',4200,'XI');
22. Write SQL statements to display all records from the above table.
Select * from student;
23. Write SQL statements to increase marks of all students by 5.
Update student set marks=marks+5;
24. Write SQL statements to delete record of student ‘Raman’.
Delete from student where name='Raman';
24. Write SQL statements to find number of students in each class.
Select class, count(*) from student group by class;
25. Write SQL statements to list name and marks of all students in descending order of their marks.
Select name, marks from student order by marks desc;