Practical

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

PRACTICAL

 PYTHON
1) Write a program to create Series from dictionary of values and an
ndarray.
CODE:-
import pandas as pd
import numpy as np
#DICTIONARY
dct={'A':12,'B':19,'C':78,'D':23,'E':76,'F':34}
S=pd.Series(dct)
print(S)
#NDARRAY
arr=np.array(['D','F','W','A','K','I','H'])
s=pd.Series(arr)
print(s)
OUTPUT:

Page 1
2) Write a code to create two series namely section and contri to store
the section (‘A’,’B’,’C’,’D’,’E’) and contribution (6700,5600,5000,5200,nil)
made by them respectively.
CODE:-
import pandas as pd
section=pd.Series(['A','B','C','D','E'])
contri=pd.Series([76,9,np.nan,23,12])
section.name='SECTION_NAME'
contri.name='AMT'
print(section.name)
print(section)
print(contri.name)
print(contri)
OUTPUT:-

Page 2
3) School has decided to contribute same amount made by each section
i.e. donation will be doubled. Write a code for the same.
CODE:-
import pandas as pd
section=pd.Series(['Aashu','Bhavesh','Charmi','Dhruv','Esha'])
contri=pd.Series([50,34,56,23,89])
section.name='SECTION_NAME'
contri.name='AMT'
print(section.name)
print(section)
print(contri.name)
print(contri)
contri=contri*2
print(contri)
OUTPUT:-

Page 3
4) Write a program to show all attributes of series. Use head and tail
functions to show 3 records respectively.
CODE:-
A=['A',2,'B',3,'C',4,'D',5,'E',6]
s=pd.Series(A)
print(s)
print(s.head(3))
print(s.tail(3))
print(s.index)
print(s.values)
print(s.dtype)
print(s.size)
print(s.ndim)
print(s.nbytes)
print(s.shape)
print(s.hasnans)
print(s.empty)
OUTPUT:-

Page 4
Page 5
5) Create DataFrame from the following data : DF
Name Age Points
Tiger 25 4.23
Jonny 26 2.24
Rinku 25 np.NaN
Vinne 23 2.56
Suresh 30 3.20

import pandas as pd
import numpy as np
D={'Name':['Tiger','Jonny','Rinku','Vinnie','Suresh'],'Age':
[25,26,25,23,30],'Points':[4.23,2.24,np.nan,2.56,3.20]}
df=pd.DataFrame(D)
print(df)
OUTPUT:-

Page 6
6) Write code to show all attributes of DataFrame DF.
CODE:-
import pandas pd
import numpy as np
D={'Name':['Tiger','Jonny','Rinku','Vinnie','Suresh'],'Age':
[25,26,25,23,30],'Points':[4.23,2.24,np.nan,2.56,3.20]}
df=pd.DataFrame(D)
print(df.index)
print(df.columns)
print(df.axes)
print(df.values)
print(df.dtypes)
print(df.size)
print(df.ndim)
print(df.empty)
print(df.T)
OUTPUT:-

Page 7
7) Create following dataframe
name-vet
Age Visits Priority
Cat 2.5 1 Yes
Cat 3 3 Yes
Snake 0.5 2 No
Dog Np.NaN 3 Yes
Dog 5 2 No
Cat 2 3 No
Snake 4.5 1 No
Cat Np.Nan 1 Yes
Dog 7 2 No

import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
print(vet)

Solve the following questions based on DataFrame- vet


a) Show age of all animals.
CODE:-

Page 8
import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
print(vet[‘Age’])
OUTPUT:-

b) Show animals cat with their priority.


CODE:-
import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
print(vet.loc['Cat','Priority'])

Page 9
OUTPUT:-

c) Show all cats and dogs with their age


CODE:-
import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
print(vet.loc[[‘Cat’,’Dog’]],’Age’)
OUTPUT:-

d) Update age of snake to 1.


CODE:-
import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],

Page
10
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
vet.loc['Snake','Age']=1
print(vet)
OUTPUT:-

e) Show first 5 records of vet


CODE:-
import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
print(vet.head())
OUTPUT:-

Page
11
f) Show sorted records by age of the animal
CODE:-
import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
print(vet.sort_values('Age'))
OUTPUT:-

g) Delete column priority


CODE:-
import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],

Page
12
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
del vet['Priority']
print(vet)
OUTPUT:-

h) drop rows with index 3 and 4


CODE:-
import pandas as pd
import numpy as np
D={'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
Name=['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog']
vet=pd.DataFrame(D,Name)
g=vet.drop([vet.index[3],vet.index[4]])
print(g)
OUTPUT:-

Page
13
Page
14
8) Create bar graph stating name and visits column from dataframe vet.
Apply suitable customization formats to the chart.
CODE:-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
D={'Name':['Cat','Cat','Snake','Dog','Dog','Cat','Snake','Cat','Dog'],
'Age':[2.5,3,0.5,np.nan,5,2,4.5,np.nan,7],
'Visits':[1,3,2,3,2,3,1,1,2],
'Priority':['Yes','Yes','No','Yes','No','No','No','Yes','No']}
vet=pd.DataFrame(D)
print(vet)
x=vet['Name']
y=vet['Visits']
clr=['r','b','g']
plt.bar(x,y,color=clr,width=0.25,edgecolor='black')
plt.title('Vet')
plt.xlabel('Name')
plt.ylabel('Visits')
plt.show()
OUTPUT:-

Page
15
Page
16
9) Create line graph for the following data
subjects=[‘Arts’,’Science’,’Commerce’] total_students=[50,56,48] apply
suitable customization formats for given chart.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
s=['Arts','Science','Commerce']
t_s=[50,56,48]
clr=['r','b','y']
plt.bar(s,t_s,color=clr,width=0.25,edgecolor='black')
plt.title('Streams')
plt.xlabel('Students')
plt.ylabel('Total_Students')
plt.show()
OUTPUT:-

Page
17
Page
18
10) Create csv file for the following data.
Employee.csv
Perform following commands based on Employee.csv

a) Read data of csv in DataFrame called emp. Print emp.


CODE:-
import pandas as pd
df=pd.read_csv("D:\\Employee.csv")
print(df)
OUTPUT:-

b) create DataFrame of first 5 records of csv.


CODE:-
import pandas as pd
emp1=pd.read_csv("D:\\Employee.csv",nrows=5)
print(emp1)
OUTPUT:-

Page
19
Page
20
 MySQL
1. Write command to create the following table Emp with details given
below
Field/column Datatype Size Constraint
Empid Int Primary Key
Ename Varchar 20 Not Null
Salary Int
Comm Int

Use db1;
Create table Emp(
Empid int Primary key,
Ename varchar(20) not null,
Salary int,
Comm int);

1)Add new column designation varchar 30 to the above table.


> alter table Emp add column designation varchar(20);
2)Give size 5 to salary column.
> alter table Emp modify Salary int (5);
3)Drop column comm.
> alter table Emp drop column Comm ;
4)Display structure of the table.
> desc Emp;

Page
21
2. Solve queries based on the following table
Table: Book
Code:-
create database PROJECT;
use PROJECT;
create table Book(
No int,
Title varchar(30),
Author varchar(15),
Type char(10),
Pub varchar(15),
Qty int,
Price int);
insert into Book values(1,’Data Structure’,’Lipschutz’,’DS’,’McGraw’,3,200),
(2,’Computer Studies’,’French’,’FND’,’Galgotia’,2,75),
(3,’Advanced Pascal’,’ Schildt’,’PROG’,’McGraw’,4,350),
(4,’Dbase dummies’,’ Palmer’,’DBMS’,’PustakM’,5,130),
(5,’Mastering C++’,’Gurewich’,’PROG’,’BPB’,3,295),
(6,’Guide Network’,’Freed’,’NET’,’ZPress’,3,200),
(7,’Mastering Foxpro’,’Seigal’,’DBMS’,’BPB’,2,135);

1) Display title and price

Page
22
2) Display title, author and price

3) Display title and price with heading “Book Name” and “Price of the
book”

Page
23
4) Show price of all the books increased by 100 with heading “ Price
increased by 100”

5) Display unique/distinct type from the table

6) Display data of author ‘french’

7) Display data of Mastering Foxpro

Page
24
8) Display titles of book that has price below 200

9) Display records of books that has quantity more than 3

10) Display book that has quantity more than 3 and price more than 150

11) Select concat(Ucase(type),pub) “show” from book where price>200;

Page
25
12) Select Substr(title,2,5) from book;

13) Select instr(“Swaminarayan”,”min”);

14) Select length(title)+price from book where price=200;

Page
26
15) Select mod(qty,2) from book;

16) Select round(234.345,2),truncate(2345.234,1);

17) Select year(curdate())+dayofweek(curdate()) “date” ;

18) Show count of books by each publication.

Page
27
19) Show max price of PROG books.

20) Show max price and publication whose max price more than 150.

Page
28

You might also like