0% found this document useful (0 votes)
24 views

IP practical file 2022

Uploaded by

khoundarunabh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

IP practical file 2022

Uploaded by

khoundarunabh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

N Da Signatu

Practical
o te re
1 Data handling using Pandas
1 Find the sum of those values which are ending with 3 or
5 in a Series
2 Create a series of 10 numbers starting with 41 and with
the increment of 3. Now add 7 all odd values and subtract
3 in even values. Reprint the updated series.
3 Create a series of 10 numbers. Change the value of all the
elements those values are multiples of 4.
4 Create a series and print the top 3 elements using the head
function.
5 Create a series and print the last 3 elements using the tail
function.
6 Create a series with these numbers: 21, 51, 71, 31, 12.
Exchange all these values of series by shifting each of
them one to one position before and by shifting the first
value to last position.
7 Create a dataframe named as students using a list of
names of 5 students
8 Create a dataframe players using a list of names and
scores of the previous three matches. (Using Nested list)
9 Create a dataframe countries using a dictionary which
stored country name, capitals and populations of the
country.
1 Iterate dataframe created in question no. 8 by its rows.
0
1 Print scores of previous two matches along with their
1 names using iterrows function. (Use dataframe created in
question 8)
1 Make a total of score from the dataframe players and
2 display their rank according their scores.
1 Print the batsman name along with runs scored in Test
3 and T20 using column names and dot notation.
1 Display the Batsman name along with runs scored in ODI
4 using loc.
1 Display the batsman details who scored
5  More than 2000 in ODI
 Less than 2500 in Test
 More than 1500 in T20
2 Part 2 Data Visualization
1 Plot following data on line chart and follow the given
instructions:
6
Day Monday Tuesday Wednesday Thursday
Income 510 350 475 580
 Write a title for the chart "The Weekly Income
Report".
 Write the appropriate titles of both the axes.
 Write code to Display legends.
 Display red color for the line.
 Use the line style - dashed
 Display diamond style markers on data points

1 Consider the following data of a medical store and plot


the data on the line chart and Customize the chart as you
7
wish:
Month Masks Sanitizer Hand wash
March 1500 4400 6500
April 3500 4500 5000
May 6500 5500 5800
June 6700 6000 6300
July 6000 5600 6200
August 6800 6300 4500
1 Use above data and subplot sanitizer data and handwash
8 data.
1 Display following bowling figures through bar chart:
9 Overs Runs
1 6
2 18
3 10
4 5
3 Part 3 MySQL queries
Consider the given table and write given queries :
1) To join product and company and display in the
tabular form like - <pname> manufactured by
<company>
2) Convert all product name into capital
3) Display the cube of products quantity more than or
100
4) Divide the price by 3 and display the result with 1
fraction digit for the price of more than 40,000.
5) Display pname (last four letters only), qty, price with
2 decimal points and company for price in between
30000 to 80000.
6) Display maximum price of products
7) Display the total quantities of all products
8) Display the average price of LED TV and Apple
products
9) Find the difference between maximum price and
minimum price from the table.
10) Display unique Products from the table.
11) Count the unique company from products.
12) Display the product number, product name and
company in the descending order of their price.
13) Display product minimum price for each company.
14) Display product number and product names in their
ascending order of names.
15) Display maximum price of products manufactured by
apple.

1. Create a series of these numbers:


33,55,65,29,19,23.
Find the sum of those values which are ending with 3 or 5.
Code:
import pandas as pd
list = [33, 55, 65, 29, 19, 23]
ser = pd.Series(list)
sum5 = sum(ser[ser%10 == 5])
sum3 = sum(ser[ser%10 == 3])
print(sum3 + sum5)

Output:
176
2. Create a series of 10 numbers starting with 41 and with the increment of
3. Now add 7 all odd values and subtract 3 in even values. Reprint the
updated series.
import pandas as pd
ser = pd.Series(range(41, 71, 3))
for i in range(0, ser.size):
if ser[i]%2==0:
ser[i] = ser[i] - 3
elif ser[i]%2!=0:
ser[i] = ser[i] + 7
print(ser)

Output:
0 48
1 41
2 54
3 47
4 60
5 53
6 66
7 59
8 72
9 65
dtype: int64

3. Create a series of 10 numbers. Change the value of all the elements those
values are multiples of 4 to 21.
import pandas as pd
numbers = []
for i in range(1,11):
val = int(input("Enter a number:"))
numbers.append(val)
ser = pd.Series(numbers)
ser[ser%4 == 0] = 21
print(ser)

Output:
Enter a number:4
Enter a number:7
Enter a number:10
Enter a number:16
Enter a number:20
Enter a number:23
Enter a number:45
Enter a number:11
Enter a number:14
Enter a number:18
0 21
1 7
2 10
3 21
4 21
5 23
6 45
7 11
8 14
9 18
dtype: int64

4. Create a series and print the top 3 elements using the head function.
import pandas as pd
ser_length = int(input("Enter the length of the series:"))
data = []
for i in range(ser_length):
val = int(input("Enter a values:"))
data.append(val)
ser = pd.Series(data)
print(ser.head(3))

Output:
Enter the length of the series:5
Enter a values:7
Enter a values:4
Enter a values:8
Enter a values:2
Enter a values:10
0 7
1 4
2 8
dtype: int64

5. Create a series and print the bottom 3 elements using the tail function.
import pandas as pd
ser_length = int(input("Enter the length of the series:"))
data = []
for i in range(ser_length):
val = int(input("Enter a values:"))
data.append(val)
ser = pd.Series(data)
print(ser.tail(3))

Output:
Enter the length of the series:7
Enter a values:4
Enter a values:8
Enter a values:2
Enter a values:6
Enter a values:9
Enter a values:10
Enter a values:12
4 9
5 10
6 12
dtype: int64

6. Create a series with these numbers: 21, 51, 71, 31, 12. Exchange all these
values of series by shifting each of them one to one position before and by
shifting the first value to last position.
import pandas as pd
import numpy as np
s = pd.Series([21, 51, 71, 31, 12])
print(pd.Series(np.roll(s.values, -1), index = s.index))

Output:
0 51
1 71
2 31
3 12
4 21
dtype: int64

7. Create a dataframe named as students using a list of names of 5 students.


import pandas as pd
students = ['Ram', 'Aman', 'Akash', 'Ruchi', 'Ravi']
stud_df = pd.DataFrame(students, columns = ['Name'])
print(stud_df)

Output
Name
0 Ram
1 Aman
2 Akash
3 Ruchi
4 Ravi

8. Create a dataframe players using a list of names and scores of the


previous three matches. (Using Nested list)
import pandas as pd
data = [['Virat', 55, 66, 31], ['Rohit', 88,66,43], ['Dhoni', 99, 58, 68]]
players= pd.DataFrame(data, columns = ['Name', 'M1', 'M2', 'M3'])
print(players)

Output:
Name M1 M2 M3
0 Virat 55 66 31
1 Rohit 88 66 43
2 Dhoni 99 58 68
9. Create a dataframe countries using a dictionary which stored country
name, capitals and populations of the country.
import pandas as pd
country_data = {"Country Name" : ["India", "Canada", "Australia"],
"Capital":["New Delhi", "Ottawa", "canberra"],
"Population":["140 Cr", "10 Cr", "50Cr"]}
countries = pd.DataFrame(country_data)
print(countries)
Output:
Country Name Capital Population
0 India New Delhi 140 Cr
1 Canada Ottawa 10 Cr
2 Australia canberra 50Cr

10. Iterate dataframe created in question no. 8 by its rows.


import pandas as pd
data = [['Virat', 55, 66, 31], ['Rohit', 88,66,43], ['Dhoi', 99, 58, 68]]
players= pd.DataFrame(data, columns = ['Name', 'M1', 'M2', 'M3'])
for index, row in players.iterrows():
print(index, row.values)

Output:
0 ['Virat' 55 66 31]
1 ['Rohit' 88 66 43]
2 ['Dhoi' 99 58 68]

11. Print scores of previous two matches along with their names using
iterrows function. (Use dataframe created in question 8)
import pandas as pd
data = [['Virat', 55, 66, 31], ['Rohit', 88,66,43], ['Dhoi', 99, 58, 68]]
players= pd.DataFrame(data, columns = ['Name', 'M1', 'M2', 'M3'])
for index, row in players.iterrows():
print(index, row["Name"], row["M1"], row["M2"], row["M3"])

Output:
0 Virat 55 66 31
1 Rohit 88 66 43
2 Dhoi 99 58 68

12. Make a total of score from the dataframe players and display their
rank according the their scores.
import pandas as pd
data = [['Virat', 55, 66, 31], ['Rohit', 88,66,43], ['Dhoi', 99, 58, 68]]
players= pd.DataFrame(data, columns = ['Name', 'M1', 'M2', 'M3'])
players["Total Score"] = players["M1"]+players["M2"]+players["M3"]
players['Rank'] = players['Total Score'].rank(ascending=0)
print(players)

Output:
Name M1 M2 M3 Total Score Rank
0 Virat 55 66 31 152 3.0
1 Rohit 88 66 43 197 2.0
2 Dhoi 99 58 68 225 1.0

13. Print the batsman name along with runs scored in Test and T20 using
column names and dot notation.
import pandas as pd
player_data = {'Name':['Virat Kohli', 'Ajinkya Rahane', 'Rohit Sharma',
'Shikhar Dhavan','Hardik Pandya'],
'Test':[3543, 2578, 2280, 2158, 1879],
'ODI':[2245, 2165, 2080, 1957, 1856],
'T20':[1925, 1853, 1522, 1020, 980]}
data = pd.DataFrame(player_data)
data.index = data.index+1
print(data.Name)
print('Test Record:')
print(data.Test)
print('T20 Record:')
print(data.T20)

Output:
1 Virat Kohli
2 Ajinkya Rahane
3 Rohit Sharma
4 Shikhar Dhavan
5 Hardik Pandya
Name: Name, dtype: object
Test Record:
1 3543
2 2578
3 2280
4 2158
5 1879
Name: Test, dtype: int64
T20 Record:
1 1925
2 1853
3 1522
4 1020
5 980
Name: T20, dtype: int64
14. Display the Batsman name along with runs scored in ODI using loc.
import pandas as pd
player_data = {'Name':['Virat Kohli', 'Ajinkya Rahane', 'Rohit Sharma',
'Shikhar Dhavan','Hardik Pandya'],
'Test':[3543, 2578, 2280, 2158, 1879],
'ODI':[2245, 2165, 2080, 1957, 1856],
'T20':[1925, 1853, 1522, 1020, 980]}
data = pd.DataFrame(player_data)
data.index = data.index+1
print(data.loc[:,('Name', 'ODI')])

Output:
Name ODI
1 Virat Kohli 2245
2 Ajinkya Rahane 2165
3 Rohit Sharma 2080
4 Shikhar Dhavan 1957
5 Hardik Pandya 1856

15. Display the batsman details who scored


a. More than 2000 in ODI
b. Less than 2500 in Test
More than 1500 in T20
import pandas as pd
player_data = {'Name':['Virat Kohli', 'Ajinkya Rahane', 'Rohit Sharma',
'Shikhar Dhavan','Hardik Pandya'],
'Test':[3543, 2578, 2280, 2158, 1879],
'ODI':[2245, 2165, 2080, 1957, 1856],
'T20':[1925, 1853, 1522, 1020, 980]}
data = pd.DataFrame(player_data)
data.index = data.index+1
print("----Runs greater than 2500 in ODI----")
print(data.loc[data['ODI']>2500, ['Name']])
print("----Runs less than 2500 in Test----")
print(data.loc[data['Test']<2500, ['Name']])
print("----Runs more than 1500 in T20----")
print(data.loc[data['T20']>1500, ['Name']])

Output:
----Runs greater than 2500 in ODI----
Empty DataFrame
Columns: [Name]
Index: []
----Runs less than 2500 in Test----
Name
3 Rohit Sharma
4 Shikhar Dhavan
5 Hardik Pandya
----Runs more than 1500 in T20----
Name
1 Virat Kohli
2 Ajinkya Rahane
3 Rohit Sharma
16. Plot following data on line chart and follow the given instructions:
Day Monday Tuesday Wednesday Thursday Friday
Income 510 350 475 580 600
a. Write a title for the chart "The Weekly Income Report".
b. Write the appropriate titles of both the axes.
c. Write code to Display legends.
d. Display red color for the line.
e. Use the line style - dashed
f. Display diamond style markers on data points

import matplotlib.pyplot as plt


day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
inc = [510, 350, 475, 580, 600]
plt.plot(day, inc, label='Income', color='r', linestyle='dashed',
marker='D')
plt.title("The Weekly Income Report")
plt.xlabel("Days")
plt.ylabel("Income")
plt.legend()
plt.show()

Output:
17. Consider the following data of a medical store and plot the data on
the line chart and Customize the chart as you wish:
Month Masks Sanitizer Hand wash
March 1500 4400 6500
April 3500 4500 5000
May 6500 5500 5800
June 6700 6000 6300
July 6000 5600 6200
August 6800 6300 4500

import matplotlib.pyplot as plt


mon = ['March', 'April', 'May', 'June', 'July', 'August']
mask = [1500, 3500, 6500, 6700, 6000, 6800]
san = [4400, 4500, 5500, 6000, 5600, 6300]
hw = [6500, 5000, 5800, 6300, 6200, 4500]
plt.plot(mon, mask, label='Mask', color='g', linestyle='dashed',linewidth=4,
marker='3', markerfacecolor='k', markeredgecolor='r')
plt.plot(mon, san, label='Sanitizer', color='b', linestyle='dashed',linewidth=4,
marker='3', markerfacecolor='k', markeredgecolor='g')
plt.plot(mon, hw, label='Handwash', color='r', linestyle='dashed',linewidth=4,
marker='v', markerfacecolor='k', markeredgecolor='b')
plt.title("Fitwell Medical Store")
plt.xlabel("Month")
plt.ylabel("Covid Protections")
plt.legend()
plt.show()

Output:

18. Use above data and subplot sanitizer data and handwash data.
import matplotlib.pyplot as plt
mon = ['March', 'April', 'May', 'June', 'July', 'August']
san = [4400, 4500, 5500, 6000, 5600, 6300]
hw = [6500, 5000, 5800, 6300, 6200, 4500]
plt.subplot(2,1,1)
plt.plot(mon, san, label='Sanitizer', color='b', linestyle='dashed',linewidth=4,
marker='o', markerfacecolor='k', markeredgecolor='r')
plt.title("Fitwell Medical Store")
plt.legend()
plt.subplot(2,1,2)
plt.plot(mon, hw, label='Handwash', color='r',
linestyle='dashed',linewidth=4, marker='v', markerfacecolor='k',
markeredgecolor='b')
plt.xlabel('Months')
plt.ylabel('Covid Protections')
plt.legend()
plt.show()

Output:

19. Display following bowling figures through bar chart:


Overs Runs
1 6
2 18
3 10
4 5

import matplotlib.pyplot as plt


over = [1,2,3,4]
runs = [6,18,10,5]
plt.bar(over,runs,color='m')
plt.xlabel("Overs")
plt.ylabel("Runs")
plt.title("Bowling Spell Analysis")
plt.show()

Output:
20. Create the following table products and write queries given below:
Table: Products
Pcode Pname Qty Price Company

P1001 iPad 120 15000 Apple

P1002 LED TV 100 85000 Sony

P1003 DSLR Camera 10 25000 Philips

P1004 iPhone 50 95000 Apple

P1005 LED TV 20 45000 MI

P1006 Bluetooth Speaker 100 20000 Ahuja

Constraints:
1. Pcode – Primary Key
2. Pname – Not Null

Create table command:


mysql> create table products
-> (pno varchar(5) primary key,
-> pname varchar(25) not null,
-> qty integer,
-> price integer,
-> company varchar(15));
Query OK, 0 rows affected (1.95 sec)

Insert Record command:


mysql> insert into products values('P1001', 'iPad', 120, 15000, 'Apple');
Query OK, 1 row affected (0.24 sec)

1. To join product and company and display in tabular form like -


<pname> manufatured by <company>
mysql> select concat(pname, concat('manufactured by ',company)) from
products;
+---------------------------------------------------+
| concat(pname, concat('manufactured by ',company)) |
+---------------------------------------------------+
| iPadmanufactured by Apple |
| LED TVmanufactured by Sony |
| DSLR Cameramanufactured by Philips |
| iPhonemanufactured by Apple |
| LED TVmanufactured by MI |
| Bluetooth Speakermanufactured by Ahuja |
+---------------------------------------------------+
6 rows in set (0.15 sec)

2. Convert all product name into capital


mysql> select ucase(pname) from products;
+-------------------------------+
| ucase(pname) |
+-------------------------------+
| IPAD |
| LED TV |
| DSLR CAMERA |
| IPHONE |
| LED TV |
| BLUETOOTH SPEAKER |
+-------------------+
6 rows in set (0.14 sec)

3. Display the cube of products quantity for more than or equal to 100
in quantity.

mysql> select qty, pow(qty, 3) from products where qty>=100;


+------+-------------+
| qty | pow(qty, 3) |
+------+-------------+
| 120 | 1728000 |
| 100 | 1000000 |
| 100 | 1000000 |
+------+-------------+
3 rows in set (0.00 sec)
4. Divide the price by 3 and display the result with 1 fraction digit for
price of more than 40000.
mysql> select round((price/3),1) from products where price>40000;
+-----------------------+
| round((price/3),1) |
+-----------------------+
| 28333.3 |
| 31666.7 |
| 15000.0 |
+-----------------------+
3 rows in set (0.02 sec)

5. Display pname (last four letters only), qty, price with 2 decimal
points and company for price in between 30000 to 80000.
mysql> select right(pname,4), qty, round(price,2),company from
products where price between 30000 and 80000;
+----------------+------+----------------+--------------+
| right(pname,4) | qty | round(price,2) | company |
+----------------+------+----------------+--------------+
| D TV | 20 | 45000 | MI |
+----------------+------+----------------+--------------+
1 row in set (0.00 sec)

6. Display maximum price of products


mysql> select max(price) from products;
+--------------+
| max(price) |
+--------------+
| 95000 |
+--------------+
1 row in set (0.15 sec)

7. Display the total quantities of all products


mysql> select sum(qty) from products;
+-----------+
| sum(qty) |
+-----------+
| 400 |
+-----------+
1 row in set (0.09 sec)

8. Display the average price of LED TV and Apple products


mysql> select avg(price) from products where pname = 'LED TV' or
company = 'Apple';
+--------------+
| avg(price) |
+--------------+
| 60000.0000 |
+--------------+
1 row in set (0.02 sec)

9. Find the difference between maximum price and minimum price


from the table.
mysql> select max(price)-min(price) from products;
+---------------------------+
| max(price)-min(price) |
+---------------------------+
| 80000 |
+---------------------------+
1 row in set (0.00 sec)

10. Display unique Products from the table.


mysql> select distinct pname from products;
+---------------------+
| pname |
+------------------- --+
| iPad |
| LED TV |
| DSLR Camera |
| iPhone |
| Bluetooth Speaker |
+----------------------+
5 rows in set (0.14 sec)

11. Count the unique company from products.


mysql> select count(distinct company) from products;
+------------------------------+
| count(distinct company) |
+------------------------------+
| 5 |
+------------------------------+
1 row in set (0.10 sec)
12. Display the product number, product name and company in the
descending order of their price.
mysql> select pno, pname, company, price from products order by
price desc;
+-------+-----------------------+---------+----------+
| pno | pname | company | price |
+-------+-----------------------+---------+----------+
| P1004 | iPhone | Apple | 95000 |
| P1002 | LED TV | Sony | 85000 |
| P1005 | LED TV | MI | 45000 |
| P1003 | DSLR Camera | Philips | 25000 |
| P1006 | Bluetooth Speaker | Ahuja | 20000 |
| P1001 | iPad | Apple | 15000 |
+-------+-------------------+---------+-------+
6 rows in set (0.10 sec)

13. Display product minimum price for each company.


mysql> select company, min(price) from products group by company;
+-----------+------------+
| company | min(price) |
+-----------+------------+
| Apple | 15000 |
| Sony | 85000 |
| Philips | 25000 |
| MI | 45000 |
| Ahuja | 20000 |
+---------+------------+
5 rows in set (0.11 sec)

14. Display product number and product names in their ascending


order of names.
mysql> select pno, pname from products order by pname;
+-------+-----------------------+
| pno | pname |
+-------+-----------------------+
| P1006 | Bluetooth Speaker |
| P1003 | DSLR Camera |
| P1001 | iPad |
| P1004 | iPhone |
| P1002 | LED TV |
| P1005 | LED TV |
+-------+-----------------------+
6 rows in set (0.00 sec)

15. Display maximum price of products manufactured by apple.


mysql> select company, max(price) from products group by company
having company = 'Apple';
+---------+---------------+
| company | max(price) |
+---------+---------------+
| Apple | 95000 |
+---------+---------------+
1 row in set (0.00 sec)

You might also like