Python Practical File 2024-25
Python Practical File 2024-25
DESC GRADUATE;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| SNO | int | YES | | NULL | |
| NAME | varchar(13) | YES | | NULL | |
| STIPEND | int | YES | | NULL | |
| SUBJECT | varchar(15) | YES | | NULL | |
| AVERAGE | int | YES | | NULL | |
| DIVISION | int | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
DESC GUIDE;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| SUBJECT | varchar(15) | YES | | NULL | |
| ADVISOR | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
INSERT INTO GRADUATE VALUES (1, 'KARAN', 400, 'PHYSICS', 68, 1);
INSERT INTO GRADUATE VALUES (2, 'DIVAKAR', 450, 'COMPUTER SC', 68, 1);
INSERT INTO GRADUATE VALUES (3, 'DIVYA', 300, 'CHEMISTRY', 62, 2);
INSERT INTO GRADUATE VALUES (4, 'ARUN', 350, 'PHYSICS', 63, 1);
INSERT INTO GRADUATE VALUES (5, 'SABINA', 500, 'MATHEMATICS', 70, 1);
INSERT INTO GRADUATE VALUES (6, 'JOHN', 400, 'CHEMISTRY', 55, 2);
INSERT INTO GRADUATE VALUES (7, 'ROBERT', 250, 'PHYSICS', 64, 1);
1|Page
SELECT * FROM GRADUATE;
+------+---------+---------+-------------+---------+---------+
| SNO | NAME | STIPEND | SUBJECT | AVERAGE | DIVISON |
+------+---------+---------+-------------+---------+---------+
| 1 | KARAN | 400 | PHYSICS | 68 | 1 |
| 2 | DIVAKAR | 450 | COMPUTER SC | 68 | 1 |
| 3 | DIVYA | 300 | CHEMISTRY | 62 | 2 |
| 4 | ARUN | 350 | PHYSICS | 63 | 1 |
| 5 | SABINA | 500 | MATHEMATICS | 70 | 1 |
| 6 | JOHN | 400 | CHEMISTRY | 55 | 2 |
| 7 | ROBERT | 250 | PHYSICS | 64 | 1 |
+------+---------+---------+-------------+---------+---------+
3. Display the details of graduates who were teached by Vinod and their average
is more than 65.
SELECT NAME, ADVISOR
FROM GRADUATE GR, GUIDE GU
WHERE (GU.SUBJECT = GR.SUBJECT) AND (ADVISOR = "VINOD") AND (AVERAGE > 65);
2|Page
+-------+---------+
| NAME | ADVISOR |
+-------+---------+
| KARAN | VINOD |
+-------+---------+
4. Display the details of graduates whose division is 2nd and their name starts
with D and ends with A.
3|Page
+---------+---------+
| NAME | STIPEND |
+---------+---------+
| DIVAKAR | 450 |
| DIVYA | 300 |
+---------+---------+
10. Show the details of graduates in the alphabetical order of their names.
11. To display the name, subject and advisor for all the graduates passing with
1st division.
4|Page
+---------+-------------+----------+---------+
| NAME | SUBJECT | DIVISION | ADVISOR |
+---------+-------------+----------+---------+
| ROBERT | PHYSICS | 1 | VINOD |
| ARUN | PHYSICS | 1 | VINOD |
| KARAN | PHYSICS | 1 | VINOD |
| DIVAKAR | COMPUTER SC | 1 | ALOK |
| SABINA | MATHEMATICS | 1 | MAHESH |
+---------+-------------+----------+---------+
12. To display the name, stipend and advisor of all the graduates who scores
minimum 60%.
13. To display the name of all the graduates whose advisor’s name starts with R.
14. To display the name, division and advisor for all graduates whose subject is
CHEMISTRY.
5|Page
15. To display the SNO and name of all graduates who are under advisor VINOD.
16. To display the name of graduates whose average is greater than the average
of DIVYA.
17. To display the name of graduates whose subject is same as that of graduate
no.7.
18. To display the name and average of all the graduates who have topped in
their subject.
6|Page
+---------+-------------+---------+
| NAME | SUBJECT | AVERAGE |
+---------+-------------+---------+
| KARAN | PHYSICS | 68 |
| DIVAKAR | COMPUTER SC | 68 |
| DIVYA | CHEMISTRY | 62 |
| SABINA | MATHEMATICS | 70 |
+---------+-------------+---------+
19. To display the name of all advisor along with number of students under them.
20. To display names of subject wise toppers along with their Advisor’s name and
subject.
21. To display the name of advisor along with total stipend paid in his subject.
7|Page
Table: CLUB
INSERT INTO CLUB VALUES( 1, 'KUKREJA', 35, 'KARATE', '2012-03-27', 10000, 'M', 20 );
INSERT INTO CLUB VALUES( 2, 'RAVINA', 34, 'KARATE', '2008-01-20', 12000, 'F', 15 );
INSERT INTO CLUB VALUES( 3, 'KARAN', 34, 'SQUASH', '2009-02-19', 20000, 'M', 2 );
INSERT INTO CLUB VALUES( 4, 'TARUN', 33, 'BASKETBALL', '2012-01-01', 15000, 'M', 16 );
INSERT INTO CLUB VALUES( 5, 'ZUBIN', 36, 'SWIMMING', '2008-01-12', 7000, 'M', 10 );
INSERT INTO CLUB VALUES( 6, 'KETAKI', 36, 'SWIMMING', '2012-02-24', 18000, 'F', 10 );
INSERT INTO CLUB VALUES( 7, 'ANKITA', 39, 'SQUASH', '2013-02-20', 22000, 'F', 5 );
INSERT INTO CLUB VALUES( 8, 'ZAREEN', 37, 'KARATE', '2010-02-22', 11000, 'M', 18 );
INSERT INTO CLUB VALUES( 9, 'KUSH', 41, 'SWIMMING', '2008-01-13', 9000, 'M', 17 );
INSERT INTO CLUB VALUES( 10, 'SHAILYA', 37, 'BASKETBALL', '2008-02-19', 17000, 'M', 17);
DESC CLUB;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| CID | int(2) | YES | | NULL | |
| CNAME | varchar(10) | YES | | NULL | |
| AGE | int(2) | YES | | NULL | |
| SPORTS | varchar(15) | YES | | NULL | |
| DOJ | date | YES | | NULL | |
| PAY | int(5) | YES | | NULL | |
| GEN | char(1) | YES | | NULL | |
| NOP | int(2) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
1.To show all information about the swimming coaches in the club.
8|Page
+------+--------+------+----------+------------+-------+------+------+
| CID | CNAME | AGE | SPORTS | DOJ | PAY | GEN | NOP |
+------+--------+------+----------+------------+-------+------+------+
| 5 | ZUBIN | 36 | SWIMMING | 2008-01-12 | 7000 | M | 10 |
| 6 | KETAKI | 36 | SWIMMING | 2012-02-24 | 18000 | F | 10 |
| 9 | KUSH | 41 | SWIMMING | 2008-01-13 | 9000 | M | 17 |
+------+--------+------+----------+------------+-------+------+------+
2. To show all information of the CLUB which coach name start with "K"
+------+---------+------+----------+------------+-------+------+------+
| CID | CNAME | AGE | SPORTS | DOJ | PAY | GEN | NOP |
+------+---------+------+----------+------------+-------+------+------+
| 1 | KUKREJA | 35 | KARATE | 2012-03-27 | 10000 | M | 20 |
| 3 | KARAN | 34 | SQUASH | 2009-02-19 | 20000 | M | 2 |
| 6 | KETAKI | 36 | SWIMMING | 2012-02-24 | 18000 | F | 10 |
| 9 | KUSH | 41 | SWIMMING | 2008-01-13 | 9000 | M | 17 |
+------+---------+------+----------+------------+-------+------+------+
5. To list the names of all coaches with their date of appointment (DATEOFAPP) in
descending order.
9|Page
+---------+------------+
| CNAME | DOJ |
+---------+------------+
| ZUBIN | 2008-01-12 |
| ZAREEN | 2010-02-22 |
| TARUN | 2012-01-01 |
| SHAILYA | 2008-02-19 |
| RAVINA | 2008-01-20 |
| KUSH | 2008-01-13 |
| KUKREJA | 2012-03-27 |
| KETAKI | 2012-02-24 |
| KARAN | 2009-02-19 |
| ANKITA | 2013-02-20 |
+---------+------------+
6. To display names of all Male coaches and their age, working in CLUB.
+--------------+
| COUNT(CNAME) |
+--------------+
| 10 |
+--------------+
8. Display details of all female coaches whose age is more than 35 and salary
between 15000 and 20000.
+------+--------+------+----------+------------+-------+------+------+
| CID | CNAME | AGE | SPORTS | DOJ | PAY | GEN | NOP |
+------+--------+------+----------+------------+-------+------+------+
| 6 | KETAKI | 36 | SWIMMING | 2012-02-24 | 18000 | F | 10 |
+------+--------+------+----------+------------+-------+------+------+
9. To display a report, showing coach name, pay, age and bonus (15% of Pay) for
all the coaches.
10 | P a g e
+---------+------+-------+---------+
| CNAME | AGE | PAY | BONUS |
+---------+------+-------+---------+
| KUKREJA | 35 | 10000 | 1500.00 |
| RAVINA | 34 | 12000 | 1800.00 |
| KARAN | 34 | 20000 | 3000.00 |
| TARUN | 33 | 15000 | 2250.00 |
| ZUBIN | 36 | 7000 | 1050.00 |
| KETAKI | 36 | 18000 | 2700.00 |
| ANKITA | 39 | 22000 | 3300.00 |
| ZAREEN | 37 | 11000 | 1650.00 |
| KUSH | 41 | 9000 | 1350.00 |
| SHAILYA | 37 | 17000 | 2550.00 |
+---------+------+-------+---------+
+------------+
| SPORTS |
+------------+
| KARATE |
| SQUASH |
| BASKETBALL |
| SWIMMING |
+------------+
11. To find number of coaches appointed for each game.
+------------+----------+
| SPORTS | COUNT(*) |
+------------+----------+
| BASKETBALL | 2 |
| KARATE | 3 |
| SQUASH | 2 |
| SWIMMING | 3 |
+------------+----------+
12. To display expenditure incurred (amount paid) for each Sport in the club.
+------------+----------+
| SPORTS | SUM(PAY) |
+------------+----------+
| BASKETBALL | 32000 |
| KARATE | 33000 |
| SQUASH | 42000 |
| SWIMMING | 34000 |
+------------+----------+
13. Display total pay earn by club gender wise also display no. of coaches in
each gender.
14. To display expenditure incurred (amount paid) for each Sport in the club.
17.To count the number of coaches who were appointed on odd dates.
+---------+
| CNAME |
+---------+
| KUKREJA |
+---------+
+---------+
| CNAME |
+---------+
| KUKREJA |
| RAVINA |
| KARAN |
| TARUN |
| KETAKI |
| ANKITA |
| ZAREEN |
| SHAILYA |
+---------+
13 | P a g e
Table: Scholar & Exam
+------------+-------+-------------+
| edate | class | subject |
+------------+-------+-------------+
| 2017-03-03 | 11 | MATHEMATICS |
| 2017-03-04 | 12 | MATHEMATICS |
| 2017-03-04 | 11 | HINDI |
| 2017-03-03 | 12 | ACCOUNTS |
| 2017-03-07 | 12 | B.ST |
| 2017-03-08 | 12 | I.P |
+------------+-------+-------------+
DESC EXAM;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| edate | date | YES | | NULL | |
| class | int(2) | YES | | NULL | |
| subject | varchar(15) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
14 | P a g e
DESC SCHOLAR;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| rno | int(2) | YES | | NULL | |
| schno | int(4) | YES | | NULL | |
| name | varchar(15) | YES | | NULL | |
| cl | int(2) | YES | | NULL | |
| sec | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
+------+-------+--------------+------+------+
| rno | schno | name | cl | sec |
+------+-------+--------------+------+------+
| 1 | 1200 | RAVI KUMAR | 12 | A |
| 1 | 1290 | MEENAKSHI | 12 | B |
| 2 | 1265 | MOHAN SINGH | 12 | C |
| 1 | 1299 | ASTHA SEHGAL | 11 | D |
| 2 | 1301 | RAMESH JAIN | 11 | A |
| 1 | 1308 | SITARAMAN | 11 | C |
+------+-------+--------------+------+------
+-------------+
| name |
+-------------+
| RAMESH JAIN |
+-------------+
+------+-------+-----------+------+------+
| rno | schno | name | cl | sec |
+------+-------+-----------+------+------+
| 1 | 1290 | MEENAKSHI | 12 | B |
+------+-------+-----------+------+------+
+------+-------+------------+------+------+
| rno | schno | name | cl | sec |
+------+-------+------------+------+------+
| 1 | 1200 | RAVI KUMAR | 12 | A |
| 1 | 1200 | RAVI KUMAR | 12 | A |
| 1 | 1200 | RAVI KUMAR | 12 | A |
+------+-------+------------+------+------+
15 | P a g e
4.SHOW THE DETAILS OF ALL THE STUDENT WHOSE CLASS IS 11 AND ORDER BY THEIR
SECTION.
+------+-------+--------------+------+------+
| rno | schno | name | cl | sec |
+------+-------+--------------+------+------+
| 2 | 1301 | RAMESH JAIN | 11 | A |
| 1 | 1308 | SITARAMAN | 11 | C |
| 1 | 1299 | ASTHA SEHGAL | 11 | D |
+------+-------+--------------+------+------+
+------+-------+--------------+------+------+
| rno | schno | name | cl | sec |
+------+-------+--------------+------+------+
| 2 | 1301 | RAMESH JAIN | 11 | A |
| 1 | 1308 | SITARAMAN | 11 | C |
| 1 | 1299 | ASTHA SEHGAL | 11 | D |
| 1 | 1200 | RAVI KUMAR | 12 | A |
| 1 | 1290 | MEENAKSHI | 12 | B |
| 2 | 1265 | MOHAN SINGH | 12 | C |
+------+-------+--------------+------+------+
+-------------+---------+------------+
| NAME | SUBJECT | EDATE |
+-------------+---------+------------+
| RAVI KUMAR | I.P | 2017-03-08 |
| RAVI KUMAR | I.P | 2017-03-08 |
| RAVI KUMAR | I.P | 2017-03-08 |
| MEENAKSHI | I.P | 2017-03-08 |
| MOHAN SINGH | I.P | 2017-03-08 |
+-------------+---------+------------+
16 | P a g e
9.COUNT THE NUMBER OF STUDENT.
10.DISPLAY THE NAME OF SUBJECT AS LAST LETTER FIRST AND REST AFTER THAT.
SELECT SUBJECT, CONCAT(RIGHT(SUBJECT, 1), LEFT(SUBJECT, LENGTH(SUBJECT) -1))
"NEW NAME" FROM EXAM;
+-------------+-------------+
| SUBJECT | NEW NAME |
+-------------+-------------+
| MATHEMATICS | SMATHEMATIC |
| MATHEMATICS | SMATHEMATIC |
| HINDI | IHIND |
| ACCOUNTS | SACCOUNT |
| B.ST | TB.S |
| I.P | PI. |
+-------------+-------------+
11. DISPLAY THE NAME OF SUBJECT AS LAST 2 ND LETTERS IN FIRST PLACE, REMOVE 3 RD
LAST LETTER AND REMAINING AFTER THAT.
12.DISPLAY THE EDATE, CLASS, SUBJECT WHOSE EXAM WILL HELD ON TUESDAY
17 | P a g e
14.DISPLAY THE LIST OF EXAM SUBJECT THAT ARE PRESENT IN THE EXAM TABLE.
SELECT DISTINCT SUBJECT FROM EXAM;
+-------------+
| SUBJECT |
+-------------+
| MATHEMATICS |
| HINDI |
| ACCOUNTS |
| B.ST |
| I.P |
+-------------+
+------------+----------+
| EDATE | COUNT(*) |
+------------+----------+
| 2017-03-03 | 2 |
| 2017-03-04 | 2 |
| 2017-03-07 | 1 |
| 2017-03-08 | 1 |
+------------+----------+
SELECT SUBJECT, CLASS, EDATE FROM EXAM WHERE SUBJECT = 'HINDI' AND CLASS = 11;
+---------+-------+------------+
| SUBJECT | CLASS | EDATE |
+---------+-------+------------+
| HINDI | 11 | 2017-03-04 |
+---------+-------+------------+
18 | P a g e
TABLE: HOSPITAL
INSERT INTO HOSPITAL VALUES (1,'ARPIT KUMAR', 62, 'M', 'SURGERY', '2016-01-21', 300);
INSERT INTO HOSPITAL VALUES (2,'ZAREENA OMAR', 22, 'F', 'ENT', '2015-12-12', 250);
INSERT INTO HOSPITAL VALUES (3,'KAREEM DURANI',32, 'M', 'ORTHOPEDIC', '2016-02-19', 200);
INSERT INTO HOSPITAL VALUES (4,'ARUN SINGH', 32, 'M', 'SURGERY', '2016-01-11', 300);
INSERT INTO HOSPITAL VALUES (5,'ZUBIN MEHTA', 30, 'M', 'ENT', '2016-01-12', 250);
INSERT INTO HOSPITAL VALUES (6, 'KETAKI VERMA', 16, 'F', 'ENT', '2016-02-24', 250);
INSERT INTO HOSPITAL VALUES (7,'ANKITA GUPTA', 29, 'F', 'CARDIOLOGY', '2016-02-20', 800);
INSERT INTO HOSPITAL VALUES (8,'ZAREEN KHAN', 45, 'F', 'GYNAECOLOGY', '2016-02-22', 300);
INSERT INTO HOSPITAL VALUES (9, 'KUSH PRATAP', 19, 'M', 'CARDIOLOGY', '2016-01-13', 800);
INSERT INTO HOSPITAL VALUES (10,'SHILPA JAIN',23,'F','NUCLEAR MEDICINE','2016-01-11',400);
Q1) NAME ALL THE PATIENTS ADMITTED IN '2016' AND IN THE MONTH OF 'JANUARY'?
19 | P a g e
+------+-------------+------+------------+------------+---------+--------+
| SNO | NAME | AGE | DEPARTMENT | ADMIT_ON | CHARGES | GENDER |
+------+-------------+------+------------+------------+---------+--------+
| 1 | Arpit Kumar | 62 | Surgery | 2016-01-21 | 300 | M |
| 4 | ArunSingh | 32 | Surgery | 2016-01-11 | 300 | M |
| 5 | Zubin Mehta | 30 | ENT | 2016-01-12 | 250 | M |
| 9 | Kush Pratap | 19 | Cardiology | 2016-01-13 | 800 | M |
+------+-------------+------+------------+------------+---------+--------+
Q5) NAME THE PATIENTS WHO WERER ADMITTED AFTER ZAREEN KHAN
SELECT DEPARTMENT, NAME,AGE FROM HOSPITAL WHERE AGE = (SELECT MIN(AGE) FROM
HOSPITAL);
20 | P a g e
+------------+--------------+------+
| DEPARTMENT | NAME | AGE |
+------------+--------------+------+
| ENT | Ketaki Verma | 16 |
+------------+--------------+------+
SELECT NAME, AGE FROM HOSPITAL WHERE AGE = (SELECT MIN(AGE) FROM HOSPITAL);
+--------------+------+
| NAME | AGE |
+--------------+------+
| Ketaki Verma | 16 |
+--------------+------+
Q9) Name the patients and their age, who are younger than zubin mehta
SELECT NAME FROM HOSPITAL WHERE ADMIT_ON = (SELECT MIN(ADMIT_ON) FROM HOSPITAL);
+--------------+
| NAME |
+--------------+
| Zareena Omar |
+--------------+
Q12) Name all the patients which were admitted on the same date as
SELECT NAME FROM HOSPITAL WHERE ADMIT_ON = (SELECT MIN(ADMIT_ON) FROM HOSPITAL
WHERE DEPARTMENT = 'SURGERY');
+------------+
| NAME |
+------------+
| Arun Singh |
+------------+
21 | P a g e
Q13) Display last name and its length for all the patients.
Q14) Display all the names having last name >= first name (length wise).
Q16) Display the date, when the 1st patient was admitted in surgery department.
Q18) . To display details of those Patient's, whose name starts with a Vowel.
23 | P a g e
Table: CRICKET & HOCKEY
DESC CRICKET;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| NAME | varchar(5) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
DESC HOCKEY;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| NAME | varchar(5) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
+-------+
| NAME |
+-------+
| ONE |
| TWO |
| FOUR |
| THREE |
| EIGHT |
+-------+
24 | P a g e
Q. Answer the following queries.
3. Display name of those students who are playing both the games.
25 | P a g e
6. DISPLAY NAME OF THOSE PEOPLE WHOSE NAME START WITH A VOWEL AND ENDS WITH A
VOWEL IN HOCKEY.
26 | P a g e
TABLE: VEHICLE & CHALLAN
INSERT INTO VEHICLE VALUES('UP78BF1234', '2012-01-16', 'DEEPAK YADAV', '255, Z-BLOCK, PANKI');
INSERT INTO VEHICLE VALUES('UP78DE8040','2014-09-12','JASNOOR KAUR','41/85, GANGA GANJ, RAMPUR');
INSERT INTO VEHICLE VALUES('UP78DC7978', '2013-05-19', 'KASHISH TIWARI', '255, X-BLOCK, PANKI');
INSERT INTO VEHICLE VALUES('UP78DX4548','2014-09-17','SAUMYA SINGH','E2/357, GANGA GANJ, SITAPUR');
INSERT INTO VEHICLE VALUES('UP78DE7011','2013-10-05','SUPRIYA DWIVEDI','123/420, ASHOK NAGAR, KANPUR' );
INSERT INTO VEHICLE VALUES('UP78GT0332','2015-08-15','VISHESH SINGH','910, GREEN GARDEN, KANPUR');
INSERT INTO VEHICLE VALUES('UP78RE1198','2016-08-23','KRITIKA NISHAD','56-G, VISHNU PURI, KANPUR');
INSERT INTO VEHICLE VALUES('UP78RZ1198','2016-09-02','JATIN SINGH','11/34, SHANTI NAGAR, KANPUR');
INSERT INTO VEHICLE VALUES('UP78ZX0420', '2016-12-23', 'GABBAR SINGH', 'GRAM RAMGARH');
27 | P a g e
+------------+------------+------------+---------+
| CHALLAN_NO | CH_DATE | REGNO | OFFENCE |
+------------+------------+------------+---------+
| 111011 | 2017-03-02 | UP78DX4548 | 101 |
| 111012 | 2017-03-02 | UP78BF1234 | 102 |
| 111013 | 2017-03-12 | UP78DE7011 | 104 |
| 111089 | 2017-04-13 | UP78DE8040 | 108 |
| 111090 | 2017-04-13 | UP78DC7978 | 101 |
| 111110 | 2017-06-22 | UP78DC7978 | 102 |
| 111489 | 2018-05-06 | UP78DE8040 | 103 |
| 111501 | 2018-07-21 | UP78DC7978 | 101 |
| 111513 | 2018-07-20 | UP78DE7011 | 106 |
| 111987 | 2019-07-29 | UP78BF1234 | 102 |
| 111990 | 2019-08-07 | UP78RZ1198 | 102 |
+------------+------------+------------+---------+
1. Display the dates of the first registration and last registration from the
table Vehicle.
28 | P a g e
+------------+
| REGNO |
+------------+
| UP78DX4548 |
| UP78GT0332 |
| UP78RZ1198 |
| UP78ZX0420 |
+------------+
5. Display the details of the vehicle(S), which were registered on a date where
MM is more than DD.
8. Display the month name (irrespective of the year) in which maximum challans
were recorded.
29 | P a g e
9. Display the date on which maximum challans were recorded.
11. Display the total number of vehicles registered after the year 2014.
12. Display the registration number of vehicles that have not received any
challan.
SELECT REGNO FROM VEHICLE WHERE REGNO NOT IN (SELECT REGNO FROM CHALLAN);
+------------+
| REGNO |
+------------+
| UP78GT0332 |
| UP78RE1198 |
| UP78ZX0420 |
+------------+
13. Display the details of vehicles whose registration number starts with
'UP78DE'
30 | P a g e
PANDAS DATAFRAME OPERATION 1
+---------+---------+-------------+---------+----------+
| NAME | STIPEND | SUBJECT | AVERAGE | DIVISION |
+---------+---------+-------------+---------+----------+
| KARAN | 400 | PHYSICS | 68 | 1 |
| DIVAKAR | 450 | COMPUTER SC | 68 | 1 |
| DIVYA | 300 | CHEMISTRY | 62 | 2 |
| ARUN | 350 | PHYSICS | 63 | 1 |
| SABINA | 500 | MATHEMATICS | 70 | 1 |
| JOHN | 400 | CHEMISTRY | 55 | 2 |
| ROBERT | 250 | PHYSICS | 64 | 1 |
+---------+---------+-------------+---------+----------+
1. Make a dictionary of lists using above table and convert it into DataFrame.
import pandas as pd
data = {"NAME" : ["KARAN", "DIVAKAR", "DIVYA", "ARUN", "SABINA", "JOHN",
"ROBERT"],
"STIPEND" : [400, 450, 300, 350, 500, 400, 250],
"SUBJECT" : ["PHYSICS", "COMPUTER SC", "CHEMISTRY", "PHYSICS",
"MATHEMATICS", "CHEMISTRY", "PHYSICS"],
"AVERAGE" : [68, 68, 62, 63, 70, 55, 64],
"DIVISION" : [1, 1, 2, 1, 1, 2, 1]
}
df = pd.DataFrame(data)
print(df)
Output:
NAME STIPEND SUBJECT AVERAGE DIVISION
0 KARAN 400 PHYSICS 68 1
1 DIVAKAR 450 COMPUTER SC 68 1
2 DIVYA 300 CHEMISTRY 62 2
3 ARUN 350 PHYSICS 63 1
4 SABINA 500 MATHEMATICS 70 1
5 JOHN 400 CHEMISTRY 55 2
6 ROBERT 250 PHYSICS 64 1
print(df.loc[df.STIPEND % 3 == 0, ["NAME"]])
Output:
NAME
1 DIVAKAR
2 DIVYA
31 | P a g e
3. Display the second to fifth rows from the DataFrame.
print(df.loc[1:4])
Output:
NAME STIPEND SUBJECT AVERAGE DIVISION
1 DIVAKAR 450 COMPUTER SC 68 1
2 DIVYA 300 CHEMISTRY 62 2
3 ARUN 350 PHYSICS 63 1
4 SABINA 500 MATHEMATICS 70 1
print(df["NAME"])
Output:
0 KARAN
1 DIVAKAR
2 DIVYA
3 ARUN
4 SABINA
5 JOHN
6 ROBERT
Name: NAME, dtype: object
5. Display the name and subject of those students whose average is more than 65.
Output:
NAME SUBJECT
0 KARAN PHYSICS
1 DIVAKAR COMPUTER SC
4 SABINA MATHEMATICS
6. Display the details of those students whose stipend is 400, 300 or 500.
Output:
NAME STIPEND SUBJECT AVERAGE DIVISION
0 KARAN 400 PHYSICS 68 1
2 DIVYA 300 CHEMISTRY 62 2
4 SABINA 500 MATHEMATICS 70 1
5 JOHN 400 CHEMISTRY 55 2
32 | P a g e
7. Display the details of those students whose division is 1 and stipend is
either 400 or 500.
Output:
NAME STIPEND SUBJECT AVERAGE DIVISION
0 KARAN 400 PHYSICS 68 1
4 SABINA 500 MATHEMATICS 70 1
print(df.columns)
Output:
Index(['NAME', 'STIPEND', 'SUBJECT', 'AVERAGE', 'DIVISION'], dtype='object')
print(df.dtypes)
Output:
NAME object
STIPEND int64
SUBJECT object
AVERAGE int64
DIVISION int64
dtype: object
print(df.head(2))
Output:
NAME STIPEND SUBJECT AVERAGE DIVISION
0 KARAN 400 PHYSICS 68 1
1 DIVAKAR 450 COMPUTER SC 68 1
print(df.tail(5))
OR
print(df.tail())
Output:
NAME STIPEND SUBJECT AVERAGE DIVISION
2 DIVYA 300 CHEMISTRY 62 2
3 ARUN 350 PHYSICS 63 1
4 SABINA 500 MATHEMATICS 70 1
5 JOHN 400 CHEMISTRY 55 2
6 ROBERT 250 PHYSICS 64 1
33 | P a g e
12. Transpose the DataFramedf.
print(df.T)
Output:
0 1 2 3 4 5 6
NAME KARAN DIVAKAR DIVYA ARUN SABINA JOHN ROBERT
STIPEND 400 450 300 350 500 400 250
SUBJECT PHYSICS COMPUTER SC CHEMISTRY PHYSICS MATHEMATICS CHEMISTRY PHYSICS
AVERAGE 68 68 62 63 70 55 64
DIVISION 1 1 2 1 1 2 1
print(df.shape)
Output:
(7, 5)
print(df.size)
Output:
35
print(df.STIPEND.max())
Output:
500
print(df.STIPEND.min())
Output:
250
34 | P a g e
6 ROBERT 250 PHYSICS 64 1
7 ROMIL 500 ACCOUNTANCY 92 1
17. Add a new column Advisor in the DataFramedf using following data:
["VINOD", "ALOK", "RAJAN", "VINOD", "MAHESH", "RAJAN", "VINOD", "JUGAL"]
Output:
NAME
0 KARAN
3 ARUN
6 ROBERT
Output:
NAME ADVISOR
7 ROMIL JUGAL
df.to_csv("E:/CLASS_12/PRACTICAL_FILE_2024-25")
35 | P a g e
PANDAS DATAFRAME OPERATION 2
import pandas as pd
data = {"Name" : ["ADITYA PAL", "PALAK AGARWAL", "KULDEEP SINGH", "NEHA", "PRAVESH",
"SUMIT PACHORI", "TANUSHKA"],
"DOB" : ["01/12/2002" , "08/11/2002", "12/02/2003", "02/05/2003",
"03/05/2002", "05/03/2001", "02/02/2002"],
"Gender" : ["M", "F", "M", "F", "M", "M", "F"],
"Mobile" : [9876543210, 9987654321, 9998765432, 9999765432, 8885963212,
8956664232, 9856472362],
"Height" : [5.8, 5.0, 5.3, 5.3, 5.5, 5.6, 5.1 ],
"Weight" : [55, 45, 48, 50, 54, 56, 50]
}
df = pd.DataFrame(data)
print(df)
b. print(df.columns)
c. print(df.ndim)
The above statement will give an error because the given values are only 6
whereas dataframe have 7 rows.
36 | P a g e
b. df.Name[7] = ['NANDITA', '2020/01/01', 'F', 9998887776, 5.3, 51]
The above statement will give an error because to add an row it is needed to
write "df.loc" instead of "df.Name".
c. df.loc[2, : ]
The above statement will show all the details of that person who is at 3rd
position in the dataframe.
print(df.loc[2, : ])
Output:
d. df.loc[3:5, : ]
The above statement will show all the details of those person who are at 4th to
6th position in the dataframe.
print(df.loc[3:5, : ])
Output:
The above statement will show all the rows of those person who are at 3rd to 5th
position in the dataframe and those columns who are between Name and Gender
including these columns.
Output:
37 | P a g e
Q4. Add One more Column 'FName' with suitable contents to the above Data Frame .
print(df)
Output:
(a)print(df.count(0))
Name 7
DOB 7
Gender 7
Mobile 7
Height 7
Weight 7
FName 7
(b)print(df.count(1))
0 7
1 7
2 7
3 7
4 7
5 7
6 7
(c)print(df.T)
0 1 2 3 4 5 6
Name ADITYA PAL PALAK AGARWAL KULDEEP SINGH NEHA PRAVESH SUMIT PACHORI TANUSHKA
DOB 01/12/2002 08/11/2002 12/02/2003 02/05/2003 03/05/2002 05/03/2001 02/02/2002
Gender M F M F M M F
Mobile 9876543210 9987654321 9998765432 9999765432 8885963212 8956664232 9856472362
Height 5.8 5.0 5.3 5.3 5.5 5.6 5.1
Weight 55 45 48 50 54 56 50
FName Arun Pal Rakesh Agarwal Pawan Singh Mudit Karan Manish Pachori Shivam
(d)print(df1.idxmax())
38 | P a g e
PANDAS DATAFRAME OPERATION 3
import pandas as pd
cars_dict = {"COMPANY" : ["BMW", "DZIRE", "NANO", "I20", "SENTRO"],
"COLOR" : ["RED", "GREEN", "RED", "GREEN", "GREEN"],
"COUNT" : [3, 9, 25, 26, 99],
"MODEL" : [ 120, 100, 125, 150, 70]
}
2.Create a DataFrame named cars, using the Dictionary car_dict created above,
with indexes
[‘C1’, ‘C2’, ‘C3’, ‘C4’, ‘C5’] also display it.
output:
COMPANY COLOR COUNT MODEL
C1 BMW RED 3 120
C2 DZIRE GREEN 9 100
C3 NANO RED 25 125
C4 I20 GREEN 26 150
C5 SENTRO GREEN 99 70
print(cars.COUNT.max())
99
output:
COMPANY COLOR COUNT MODEL
C4 I20 GREEN 26 150
C5 SENTRO GREEN 99 70
output:
COMPANY
C1 BMW
C3 NANO
39 | P a g e
6) Display COMPANY of all the GREEN color cars having COUNT > 10.
output:
COMPANY
C4 I20
C5 SENTRO
print(cars.loc[cars.COUNT % 2 == 1, ["COMPANY"]])
output:
COMPANY
C1 BMW
C2 DZIRE
C3 NANO
C5 SENTRO
8) Insert a row after SENTRO with index ‘C6’ and data as given in the list:
[ 'BALENO', 'RED', 65, 23 ]
10) Create a new Dataframe named Green_car, by copying all the rows from
the DataFrame cars, where value of COLOR column is GREEN.
output:
COMPANY COLOR COUNT MODEL
C2 DZIRE GREEN 9 100
C4 I20 GREEN 26 150
C5 SENTRO GREEN 99 70
40 | P a g e
PANDAS DATAFRAME OPERATION 4
import pandas as pd
+------+---------+------+------------+------------+-------+------+------+
| CID | CNAME | AGE | SPORTS | DOJ | PAY | GEN | NOP |
+------+---------+------+------------+------------+-------+------+------+
| 1 | KUKREJA | 35 | KARATE | 2012-03-27 | 10000 | M | 20 |
| 2 | RAVINA | 34 | KARATE | 2008-01-20 | 12000 | F | 15 |
| 3 | KARAN | 34 | SQUASH | 2009-02-19 | 20000 | M | 2 |
| 4 | TARUN | 33 | BASKETBALL | 2012-01-01 | 15000 | M | 16 |
| 5 | ZUBIN | 36 | SWIMMING | 2008-01-12 | 7000 | M | 10 |
+------+---------+------+------------+------------+-------+------+------+
Create a dataframe
club = {"CNAME" : ["Kukreja", "Ravina", "Karan", "Tarun", "Zubin"],
"AGE" : [35, 34, 34, 33,36],
"SPORTS" : ["KARATE","KARATE", "SQUASH", "BASKETBALL", "SWIMMING"],
"DOJ" : ["2012-03-27","2008-01-20","2009-02-19","2012-01-01","2008-01-
12"],
"PAY" : [10000,12000,20000,15000,7000],
"GEN" : ["M","F","M","M","M"],
"NOP" : [20,15,2,16,10]
}
df = pd.DataFrame(club)
print(df)
1.Add a column "Location" to the dataframe club with values "India" for all.
df["location"] = 'India'
41 | P a g e
2.Increase the pay of Karan by 4000.
df.loc[df.CNAME == "Karan","PAY"] += 4000
AGE CNAME
0 35 Kukreja
1 34 Ravina
2 34 Karan
3 33 Tarun
4 36 Zubin
36
output:
0 KARATE
1 KARATE
2 SQUASH
3 BASKETBALL
4 SWIMMING
42 | P a g e
8.Display the total pay of the table club.
df["PAY"].sum()
64000
output:
df.loc[:,["CNAME","PAY"]]
output:
CNAME PAY
0 Kukreja 10000
1 Ravina 12000
2 Karan 20000
3 Tarun 15000
4 Zubin 7000
43 | P a g e
PANDAS DATAFRAME OPERATION 5
1. Create a Dictionary of above data (Age, Phy, Chem, Math) & name it SecA.
import pandas as pd
SecA = {'Age':[17,19,17,18,16],
'Phy':[67.5,77,56.5,39,78],
'Chem':[77,72.5,69.5,46,49],
'Math':[98,65.5,53.5,91,73.5]
}
{'Age': [17, 19, 17, 18, 16], 'Phy': [67.5, 77, 56.5, 39, 78], 'Chem': [77, 72.5, 69.5,
46, 49], 'Math': [98, 65.5, 53.5, 91, 73.5]}
2. Create a DataFrame named marks, using dictionary SecA with index (Tom, James,
Ricky, Vin, Steve).
output:
Age Phy Chem Math
Tom 17 67.5 77.0 98.0
James 19 77.0 72.5 65.5
Ricky 17 56.5 69.5 53.5
Vin 18 39.0 46.0 91.0
Steve 16 78.0 49.0 73.5
marks['Total'] = 0
output ::
Age Phy Chem Math Total
Tom 17 67.5 77.0 98.0 0
James 19 77.0 72.5 65.5 0
Ricky 17 56.5 69.5 53.5 0
Vin 18 39.0 46.0 91.0 0
Steve 16 78.0 49.0 73.5 0
marks.loc['Vin','Chem']+= 8
output:
Age Phy Chem Math Total
Tom 17 67.5 77.0 98.0 0
James 19 77.0 72.5 65.5 0
Ricky 17 56.5 69.5 53.5 0
Vin 18 39.0 54.0 91.0 0
Steve 16 78.0 49.0 73.5 0
44 | P a g e
5. Update the column Total, with the sum of marks of all 3 subjects.
output:
Age Phy Chem Math Total
Tom 17 67.5 77.0 98.0 242.5
James 19 77.0 72.5 65.5 215.0
Ricky 17 56.5 69.5 53.5 179.5
Vin 18 39.0 54.0 91.0 184.0
Steve 16 78.0 49.0 73.5 200.5
print(marks.Total['Ricky']) or print(marks.loc['Ricky','Total'])
179.5
print(marks.loc['Tom']) or print(marks.loc[['Tom']])
Output:
print(marks[['Age', 'Total']])
output:
Age Total
Tom 17 242.5
James 19 215.0
Ricky 17 179.5
Vin 18 184.0
Steve 16 200.5
9. Display Name, Age & Total of students, whose Age is more than 17 years.
output:
Age Total
James 19215.0
Vin 18 184.0
45 | P a g e
10. Display details of all students getting 1st division in Phy& Math.
output:
del marks['Age']
output:
12. Display the details of students who scored more than 80 marks in any
subject.
print(marks.loc[(marks['Phy'] > 80) | (marks['Chem'] > 80) | (marks['Math'] >
80)])
output:
output:
14. Display the details of students who passed in all subjects (above 33 in all
subjects).
print(passed_students)
46 | P a g e
output:
marks['Grade'] = ''
marks.loc[marks['Percentage'] >= 90, 'Grade'] = 'A+'
marks.loc[(marks['Percentage'] >= 80) & (marks['Percentage'] < 90), 'Grade'] =
'A'
marks.loc[(marks['Percentage'] >= 70) & (marks['Percentage'] < 80), 'Grade'] =
'B'
marks.loc[(marks['Percentage'] >= 60) & (marks['Percentage'] < 70), 'Grade'] =
'C'
marks.loc[marks['Percentage'] < 60, 'Grade'] = 'D'
print(marks)
output:
47 | P a g e
PANDAS DATAFRAME OPERATION 6
SL = pd.Series(L)
print(SL)
0 a
1 e
2 i
3 o
4 u
dtype: object
head() : It displays the first n members of the DataFrame. If the values for n is not
passed, then by default n takes 5 and the first five members are displayed.
tail() : It displays the last n members of the DataFrame. If the values for n is not
passed, then by default n takes 5 and the last five members are displayed.
+-------------+---------+------------+
| Name | TMarks | City |
+-------------+---------+------------+
| Amit Kumar | 450 | New Delhi |
| Asha Goel | 426 | Bengaluru |
| Kavita | 476 | Chennai |
| Riya | 446 | Jaipur |
| Piyush | 464 | Mumbai |
+-------------+---------+------------+
48 | P a g e
(a) Write a Python statement to create a Data Frame df by using the Dictionary
StdX.
print(df)
Name TMarks City
0 Amit Kumar 450 New Delhi
1 Asha Goel 426 Benaluru
2 Kavita 476 Chennai
3 Riya 446 Jaipur
4 Piyush 464 Mumbai
print(df.head(3))
print(df.tail(2))
print(df.count())
Name 5
TMarks 5
City 5
dtype: int64
49 | P a g e
data2 = {"Name" : ["Ishan", "Amrita"],
"TMarks" : [435, 432],
"City" : ["New Delhi", "Kolkata"]}
df1 = pd.DataFrame(data2)
df = df.append(df1, ignore_index = "True")
(h) Add to df , a new column called Age, with the following data:
[12, 15, 16, 13, 14, 15, 13]
df = df.drop("City", axis = 1)
df = df.drop(3, axis = 0)
df.iloc[0:3]
Name TMarks City
0 Amit Kumar 450 New Delhi
1 Asha Goel 426 Benaluru
2 Kavita 476 Chennai
Q6. Name the pandas functions/methods that iss used to load a csv file to
pandas.
read_csv
50 | P a g e
PANDAS DATAFRAME OPERATION 7
import pandas as pd
Year1 = {"Q1" : 5000, "Q2" : 8000, "Q3" : 12000, "Q4" : 18000 }
Year2 = {"A" : 13000, "B" : 14000, "C" : 12000 }
totSales = {1 : Year1, 2 : Year2}
df = pd.DataFrame(totSales)
print(df)
1 2
Q1 5000.0 NaN
Q2 8000.0 NaN
Q3 12000.0 NaN
Q4 18000.0 NaN
A NaN 13000.0
B NaN 14000.0
C NaN 12000.0
print(df.index)
print(df.columns)
print(df[1].count())
51 | P a g e
PANDAS DATAFRAME OPERATION 8
import pandas as pd
data = {"Name" : ["Nancy Drew", "Hardy Boys", "Diary of a wimpy kid",
"Harry Potter"],
"Price" : [150, 180, 225, 500]}
df1 = pd.DataFrame(data)
print(df1)
Name Price
0 Nancy Drew 150
1 Hardy Boys 180
2 Diary of a wimpy kid 225
3 Harry Potter 500
Q4. Add a new book named "The Secret" having price 800.
Name Price
0 Nancy Drew 150
1 Hardy Boys 180
2 Diary of a wimpy kid 225
3 Harry Potter 500
4 The Secret 800
52 | P a g e
PANDAS DATAFRAME OPERATION 9
import pandas as pd
data = [["PPS", 40, 32, 8],
["JPS", 30, 18, 12],
["GPS", 20, 18, 2],
["MPS", 18, 10, 8],
["BPS", 28, 20, 8]]
(a) df3.shape
(5, 4)
(b) df3[2:4]
Q4. Write the python statement to display the data of Topper column of indexes
CO2 to CO4.
Topper
CO2 18
CO3 18
CO4 10
CO5 20
Q5. Write the python statement to compute and display the difference of data of
Tot_students column and First_Runnerup column of the above given DataFrame.
print(df3.loc[df.Tot_students - df.First_Runnerup])
53 | P a g e
PANDAS DATAFRAME OPERATION 10
Create the DataFrame Sales containing year wise sales figures for five sales
persons in INR.
Use the years as column labels and sales person names as row labels.
import pandas as pd
D = { 2014 : [100.5,150.8,200.9,30000,40000],
2015 : [12000,18000,22000,30000,45000],
2016 : [20000,50000,70000,100000,125000],
2017 : [50000,60000, 70000, 80000, 90000]
}
Sales= pd.DataFrame(D, index = ['Madhu', 'Kusum', 'Kinshuk', 'Ankit',
'Shruti'])
print(Sales)
print(sales.tail(2))
print(sales.iloc[:,:2])
c) Create a dictionary using the following data. Use this dictionary to create a
DataFrame Sales2.
import pandas as pd
D = {2018:[160000,110000,500000,340000,900000]}
Sales2 = pd.DataFrame(D, index = ['madhu', 'kusum', 'kinshuk', 'ankit',
'shruti'])
print(Sales)
2014 2015 2016 2017
Madhu 100.5 12000 20000 50000
Kusum 150.8 18000 50000 60000
Kinshuk 200.9 22000 70000 70000
Ankit 30000.0 30000 100000 80000
Shruti 40000.0 45000 125000 90000
54 | P a g e
(d) Display the row labels of Sales
sales.index
sales.columns
sales.dtypes
55 | P a g e
PANDAS sERIES OPERATIONS
a) Anaglyph, having 26 elements with the alphabets as values and default index
values.
import pandas as pd
Anaglyph = pd.Series(chr(i) for i in range(97,123))
print(Anaglyph)
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
9 j
10 k
11 l
12 m
13 n
14 o
15 p
16 q
17 r
18 s
19 t
20 u
21 v
22 w
23 x
24 y
25 z
dtype: object
b) Vowels, having 5 elements with index labels ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’ and
all the five values set to zero.
import pandas as pd
Vowels = pd.Series( 0, ["a ", "e ", "i ", "o ", "u "])
print(Vowels)
a 0
e 0
i 0
o 0
u 0
dtype: int64
c) Friends, from a dictionary having roll numbers of 5 of your friends as data &
their 1st name as keys.
import pandas as pd
Friends = pd.Series({"ram ":1, "hari ":2,"raheem ":3, "kabir ":4, "rasool":5})
print(Friends)
ram 1
hari 2
raheem 3
kabir 4
rasool 5
dtype: int64
56 | P a g e
d) MT series, an empty Series. Check if it is an empty series.
import pandas as pd
MT = pd.Series()
e) MonthDays, from a numpy array having the number of days in the 12 months of a year. The labels
should be the month numbers from 1 to 12.
import pandas as pd
import Numpy as np
MonthDays = pd.Series(np.array([31,28,31,30,31,30,31,31,30,31,30,31]),range(1,13))
print(MonthDays)
1 31
2 28
3 31
4 30
5 31
6 30
7 31
8 31
9 30
10 31
11 30
12 31
dtype: int32
Vowels[:] = 10 print(Vowels)
a 10
e 10
i 10
o 10
u 10
dtype: int64
import pandas as pd
Vowels1 = pd.Series([2,5,6,3,8],['a','e','i','o','u'])
print(Vowels1)
57 | P a g e
a 2
e 5
i 6
o 3
u 8
dtype: int64
import pandas as pd
Vowels = pd.Series(0,['a','e','i','o','u'])
Vowels1 = pd.Series([2,5,6,3,8],['a','e','i','o','u'])
Vowels3 = Vowels + Vowels1
print(Vowels1 – Vowels)
a 2
e 5
i 6
o 3
u 8
dtype: int64
print(Vowels1 *Vowels)
a 0
e 0
i 0
o 0
u 0
dtype: int64
print(Vowels/ Vowels1)
a 0.0
e 0.0
i 0.0
o 0.0
u 0.0
dtype: float64
58 | P a g e
Matplotlib Bargraph 1
59 | P a g e
Matplotlib Bargraph 2
60 | P a g e
Matplotlib Bargraph 3
Q. Make a bar chart using matplotlib showing temperatures during Morning and Evening in a week.
61 | P a g e
Matplotlib Linechart 1
62 | P a g e
Matplotlib Linechart 1
63 | P a g e
Matplotlib Linechart 3
Q. Plot a line chart using matplotlib showing the fall of wickets of both the Teams.
64 | P a g e