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

Practical File - Ip

The document contains 18 questions asking to write SQL codes to query and manipulate data from tables. Questions include filtering records, aggregating data, sorting, joining tables, updating records, and altering table schemas.

Uploaded by

Rishabh Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Practical File - Ip

The document contains 18 questions asking to write SQL codes to query and manipulate data from tables. Questions include filtering records, aggregating data, sorting, joining tables, updating records, and altering table schemas.

Uploaded by

Rishabh Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

LIST OF ASSIGNMENTS TO BE DONE/WRITTEN ON THE REPORT FILE

SUBJECT: INFORMATICS PRACTICES (065)

SL.NO QUESTIONS ON PYTHON PROGRAM


SQL CODES
Consider the following tables and write SQL Codes based on the tables:
Table: Employee
EMPNO NAME DOJ DEPT DESIG
E019 Ajay Kumar Singh 01-03-2009 Sales Executive
E183 Binod Shriwas 03-04-2011 Sales Manager
E656 Mohan Soni 11-05-1999 Marketing Manager
E763 Dilip Sharma 07-11-2006 Marketing Advisor
E263 Shreya Basu 08-12-2003 Technical Assistant
E711 Ronita Chowdhury 12-01-2013 Sales Executive
E561 Pallabi Sharma 03-12-2008 Finance Officer
E113 Vikram Kumar 11-11-2005 Technical Officer

Table: Pay

EMPNO MONTH BASIC TOTAL


E019 November 6900 8200
E019 December 6900 8400
E763 October 5200 6500
E711 September 8000 9600
E561 October 8000 10000
E711 November 16000 28000
E561 December 16000 29500
E263 August 12500 15600

1. Write SQL Code to display the details of all Marketing Advisor.


Ans:

2. Write SQL Code to display the name of the employees in the descending order of their
DOJ.
Ans:
3. Write SQL Code to display the sum of total in the month of November.
Ans:

4. Write SQL Code to display the Dept and Desig of those employees whose name starts with
‘P’.
Ans:

5. Write SQL Code to display Desig of those employees whose name is not ending with ‘a’.
Ans:
6. Write SQL Code to display the total of the employees in the ascending order of basic in
the month of October.
Ans:

7. Write SQL Code to count the number of unique department.


Ans:

8. Write SQL Code to display the name of the employees in Sales department.
Ans:

9. Write SQL Code to display the average total of those whose basic is above 8000.
Ans:

10. Write SQL Code to display the name and total number of employees department wise.
Ans:
11. Write SQL Code to display the name and basic of the employees with their matching
EMPNO in both the tables.
Ans:

12. Write SQL Code to display the name of the employees who got the maximum. total.
Ans:

13. Write SQL Code to display the basic of all Executive, Manager and Advisor.
Ans:

14. Write SQL Code to display the name of all Officers in Upper Case.
Ans:
15. Write SQL Code to display the sum of basic of all employees except E561.
Ans:

16. Write SQL Code to display the name and desig of the employees joined in the year 2013.
Ans:

17. Write SQL Code to display the name of the employees whose dept is entered.
Ans:

18. Write SQL Code to display the name of the employees whose total is not entered.
Ans:
Write SQL Code to count the number of Managers.
19. Ans:

20. Write SQL Code to increase the total of all employees in Finance department by 500.
Ans:

21. Write SQL Code to increase the total of all employees by 7%.
Ans:

22. Write SQL Code to add a new column called ADDRESS of VARCHAR (20) in the employee
table.
Ans:

23. Write SQL Code to delete the column MONTH from the PAY Table.
Ans:

24. Write SQL Code to delete all the records from the PAY Table
Ans:
PROGRAMS ON PYTHON PANDAS
1. Write a Python Program to store some elements in a Series Object and print the mean
value and the Upper Quartile(75%)
Ans:
import pandas as pd
L=eval(input('Enter 6 integers in the list:'))
L.sort()
S=pd.Series(L,index=[1,2,3,4,5,6])
print(S)
print('Mean Value=',S.mean())
print('75% Quantile',S.quantile(.75))

Output:
Enter 6 integers in the list:[56,12,45,67,99,66]
1 12
2 45
3 56
4 66
5 67
6 99
dtype: int64
Mean Value= 57.5
75% Quantile 66.75
2. Write a Python Program to create a Series containing integers and display the 1 st three
and the last three elements of the Series.
Ans:
import pandas as pd
import numpy as np
L=np.arange(10,31,3)
s=pd.Series(L)
print('1st 3 elements=\n')
print(s.head(3))
print('Last 3 elements=\n')
print(s.tail(3))

Output:
1st 3 elements=

0 10
1 13
2 16
dtype: int32
Last 3 elements=

4 22
5 25
6 28
dtype: int32
3. Write a Python Program to store some integer elements in a Series Object and display the
elements which are above 40.
Ans:
import pandas as pd
S=pd.Series([38,23,68,34,91,20,56,88],index=[1,2,3,4,5,6,7,8])
print(S)
print('Elements above 40:')
print(S[S>40])
Output:
1 38
2 23
3 68
4 34
5 91
6 20
7 56
8 88
dtype: int64
Elements Above 40:
3 68
5 91
7 56
8 88
dtype: int64
4. Write a Python Program to create two integer series and perform addition, subtraction,
multiplication and division on these two Series.
Ans:
import pandas as pd
S1=pd.Series([23,45,77,33],index=[1,2,3,4])
S2=pd.Series([31,85,17,15],index=[1,2,3,4])
print(S1)
print(S2)
print('Addition of the two Series=\n',S1+S2)
print('Subtraction of the two Series=\n',S1-S2)
print('Multiplication of the two Series=\n',S1*S2)
print('Division of the two Series=\n',S1/S2)

Output:
1 23
2 45
3 77
4 33
dtype: int64
1 31
2 85
3 17
4 15
dtype: int64
Addition of the two Series=
1 54
2 130
3 94
4 48
dtype: int64
Subtraction of the two Series=
1 -8
2 -40
3 60
4 18
dtype: int64
Multiplication of the two Series=
1 713
2 3825
3 1309
4 495
dtype: int64
Division of the two Series=
1 0.741935
2 0.529412
3 4.529412
4 2.200000
dtype: float64
5. Write a Python Program to create a dataframe from a Numpy Array and print the
dataframe. Also display the dataframe in Transpose form.
Ans:
import pandas as pd
import numpy as n
arr=n.array([[1,2,3],[4,5,6],[7,8,9],[0,0,0]])
D=pd.DataFrame(arr, columns=['A','B','C'], index=['I','II','III','IV'])
print(D)
print(D.T)

Output:
A B C
I 1 2 3
II 4 5 6
III 7 8 9
IV 0 0 0

I II III IV
A 1 4 7 0
B 2 5 8 0
C 3 6 9 0
6. Write a Python Program to create two dataframes with integers. Create a new dataframe
by adding the two dataframes. If there are any NaN values in the new dataframe, then
drop that particular row in the new dataframe.
Ans:
import pandas as p
L1=[10,20,30,90]
L2=[40,50,60]
D1=p.DataFrame(L1,columns=['A'],index=[1,2,3,4])
print(D1)
D2=p.DataFrame(L2,columns=['A'],index=[1,2,3])
print(D2)
D3=D1.add(D2)
print('New DataFrame')
print(D3)
print('New DataFrame after removing NaN Values')
D3=D3.dropna()
print(D3)

Output:
A
1 10
2 20
3 30
4 90
A
1 40
2 50
3 60
New DataFrame
A
1 50.0
2 70.0
3 90.0
4 NaN
New DataFrame after removing NaN Values
A
1 50.0
2 70.0
3 90.0
7. Write a Python Program to create two dataframes with integers. Create a new dataframe
by adding the two dataframes. If there are any NaN values in the new dataframe, then
replace the NaN values by -1. Print the updated DataFrame.
Ans:
import pandas as p
L1=[10,20,30,90]
L2=[40,50,60]
D1=p.DataFrame(L1,columns=['A'],index=[1,2,3,4])
print(D1)
D2=p.DataFrame(L2,columns=['A'],index=[1,2,3])
print(D2)
D3=D1.add(D2)
print('New DataFrame')
print(D3)
print('New DataFrame after replacing NaN Values')
D3=D3.fillna(-1)
print(D3)

Output:
A
1 10
2 20
3 30
4 90
A
1 40
2 50
3 60
New DataFrame
A
1 50.0
2 70.0
3 90.0
4 NaN
New DataFrame after replacing NaN Values
A
1 50.0
2 70.0
3 90.0
4 -1.0
8. Write a Python Program to create a Series from a dictionary containing students’ names
and their marks. Print the series. Also display the maximum, minimum and average marks
of the students.
Ans:
import pandas as pd
D={'Pratham':81,'Tina':73,'Gaurav':91,'Meera':71,'Rishav':63}
S=pd.Series(D)
print(S)
print('Highest Marks=',S.max())
print('Lowest Marks=',S.min())
print('Average Marks=',S.mean())

Output:
Gaurav 91
Meera 71
Pratham 81
Rishav 63
Tina 73
dtype: int64

Highest Marks = 91
Lowest Marks = 63
Average Marks = 75.8
9. Write a Python Program to store integer elements in a dataframe of 4 X 4 dimension and
print-
i) the original dataframe
ii) the mean of the elements in each column
iii) the mean of the elements in each row
Ans:
import pandas as pd
L=[[20,45,12,98],[56,78,45,12],[7,56,28,82],[56,23,99,32]]
df=pd.DataFrame(L,index=[1,2,3,4],columns=['A','B','C','D'])
print(df)
print('Mean of Each Column=\n',df.mean(axis=0))
print('Mean of Each Row=\n',df.mean(axis=1))

Output:
A B C D
1 20 45 12 98
2 56 78 45 12
3 7 56 28 82
4 56 23 99 32
Mean of Each Column=
A 34.75
B 50.50
C 46.00
D 56.00
dtype: float64
Mean of Each Row=
1 43.75
2 47.75
3 43.25
4 52.50
dtype: float64
10. Write a Python Program to create a dataframe containing Name and Year of Passing. After
creating the dataframe add a new column called “Percentage” with appropriate values.
Display the dataframe and also display the name of the person having maximum
percentage.
Ans:
import pandas as pd
dx={'Name':['Sayan','Mohit','Richa','Suhani','Ananya'],'Year':[2005,2005,2007,2009,2006]}
df=pd.DataFrame(dx,index=[1,2,3,4,5])
print(df)
df['Percentage']=[93.5,78.5,88.0,93.0,83.5]
print(df)
print('Topper=')
print(df['Name'][df['Percentage']==df['Percentage'].max()])

Output:

Name Year
1 Sayan 2005
2 Mohit 2005
3 Richa 2007
4 Suhani 2009
5 Ananya 2006

Name Year Percentage


1 Sayan 2005 93.5
2 Mohit 2005 78.5
3 Richa 2007 88.0
4 Suhani 2009 93.0
5 Ananya 2006 83.5

Topper=
1 Sayan
Name: Name, dtype: object
11. Write a Python Program to create a dataframe containing Students name and their marks
in Term1 and Term2. Add a new column in the dataframe called Total which will store the
sum of Term1 and Term 2. Display the details of the dataframe in the descending order of
Total.
Ans:
import pandas as pd
dx={'Name':['George','Emily','Praveen','Trina','Jeniffer'],'Term1':[42,29,37,18,34],'Term2':[24,41,33,
29,45]}
df=pd.DataFrame(dx)
df['Total']=[66,70,70,47,79]
print(df.sort_values(by='Total',ascending=False))

Output:
Name Term1 Term2 Total
4 Jeniffer 34 45 79
1 Emily 29 41 70
2 Praveen 37 33 70
0 George 42 24 66
3 Trina 18 29 47
12. Write a Python Program to create two dataframes containiing integers only. Print both
the dataframe separately. Create the 3rd dataframe by concatenating the previous two
dataframes vertically. Create the 4th dataframe by concatenating the previous two
dataframes horiznontally. Print the 3rd and the 4th dataframes.
Ans:
import pandas as p
X=[[10,20,30],[40,50,100]]
D1=p.DataFrame(X,columns=['A','B','C'],index=[1,2])
print(D1)
Y=[[60,70,80],[90,100,200]]
D2=p.DataFrame(Y,columns=['A','B','C'],index=[1,2])
print(D2)
D3=p.concat([D1,D2])
print(D3)
D4=p.concat([D1,D2],axis=1)
print(D4)

Output:

A B C
1 10 20 30
2 40 50 100

A B C
1 60 70 80
2 90 100 200

A B C
1 10 20 30
2 40 50 100
1 60 70 80
2 90 100 200

A B C A B C
1 10 20 30 60 70 80
2 40 50 100 90 100 200
13. Consider two dataframes df1 and df2 with following data.

Write a Python Program to:


i) Create the above 2 dataframes.
ii) Add the above 2 dataframes
iii) Rename the index of df2 from a,b,c to u,v,w respectively.
iv) Display maximum and average value of each row of df2.
Ans:
import pandas as pd
dx1={'p':[57,68,74,80],'q':[77,5,40,17],'r':[13,92,18,39],'s':[62,24,37,60]}
df1=pd.DataFrame(dx1,index=['a','b','c','d'])
print(df1)
dx2={'p':[0,-1,-3],'q':[0,-2,-7],'r':[0,2,1],'s':[0,4,4]}
df2=pd.DataFrame(dx2,index=['a','b','c'])
print(df2)
print(df1.add(df2))
df2=df2.rename(index={'a':'u','b':'v','c':'w'})
print(df2)
print('Maximum each row')
print(df2.max(axis=1))
print('Mean of each row')
print(df2.mean(axis=1))
Output:
p q r s
a 57 77 13 62
b 68 5 92 24
c 74 40 18 37
d 80 17 39 60

p q r s
a 0 0 0 0
b -1 -2 2 4
c -3 -7 1 4

p q r s
a 57.0 77.0 13.0 62.0
b 67.0 3.0 94.0 28.0
c 71.0 33.0 19.0 41.0
d NaN NaN NaN NaN

p q r s
u 0 0 0 0
v -1 - 2 2 4
w -3 -7 1 4

Maximum each row


u 0
v 4
w 4
dtype: int64
Mean of each row
u 0.00
v 0.75
w -1.25
dtype: float64
14. Consider the file Product.CSV as given below:
Unit
Product Company Price Quanity Total
Pendrive iBall 550 12 6600
Laptop Dell 27000 6 162000
Wireless Speaker Boat 2700 14 37800
Buletooth
Headset iBall 1250 16 20000
SmartPhone Samsung 14500 10 145000

Write a Python Program to read from the above CSV File and display the information
through a dataframe object. Also display the details of product belong to “iBall”.
Ans:
import pandas as pd
df=pd.read_csv("D:\\Product.csv")
print(df)
print(df[df['Company']=='iBall'])

Output:
Product Company Unit Price Quantity Total
0 Pendrive iBall 550 12 6600
1 Laptop Dell 27000 6 162000
2 Wireless Speaker Boat 2700 14 37800
3 Buletooth Headset iBall 1250 16 20000
4 SmartPhone Samsung 14500 10 145000

Product Company Unit Price Quantity Total


0 Pendrive iBall 550 12 6600
3 Buletooth Headset iBall 1250 16 20000

15. Write a Python program to store Name and Amount received for duty in a dataframe. Use
Pivot table to calculate the total amount received by each person. Also calculate the
number of duties given by each person.
Ans:
import pandas as pd
invg={'Name':['Naveen','Kreeti','Sanya','Kreeti','Akash','Naveen'],'Amount':[430,275,430,560,230,356
]}
df=pd.DataFrame(invg,index=[1,2,3,4,5,6])
print(df)
pvt1=df.pivot_table(index=['Name'],values=['Amount'],aggfunc='sum',margins=True,margins_name
='Sum Values')
print(pvt1)
pvt2=pd.pivot_table(df,index=['Name'],values=['Amount'],aggfunc='count',margins=True,margins_
name='Total')
print(pvt2)

Output:
Amount Name
1 430 Naveen
2 275 Kreeti
3 430 Sanya
4 560 Kreeti
5 230 Akash
6 356 Naveen
Amount
Name
Akash 230
Kreeti 835
Naveen 786
Sanya 430
Sum Values 2281

Amount
Name
Akash 1
Kreeti 2
Naveen 2
Sanya 1
Total 6
16. A Dataframe df stores about passengers’ flights and years. First few rows of data frame
are shown below:
Year Month Passenger
0 2009 January 112
1 2009 February 118
2 2007 March 132
3 2007 April 129
4 2006 May 121

Write a Python Program to:


i) Create the above Dataframe
ii) Compute total passengers per year.
iii) Compute average passengers per month.
iv) Print no of passengers in April

Ans:
import pandas as pd
info={'Year':[2009,2009,2007,2007,2006],'Month':['January','February','March','April','May'],'Passeng
er':[112,118,132,129,121]}
df=pd. DataFrame(info)
print(df.pivot_table(index='Year',values='Passenger',aggfunc='sum'))
print(df.pivot_table(index='Month',values='Passenger',aggfunc='mean'))
print('No of Passengers in April')
print(df['Passenger'][df['Month']=='April'])

Output:
Passenger
Year
2006 121
2007 261
2009 230
Passenger
Month
April 129
February 118
January 112
March 132
May 121
No of Passengers in April
3 129
Name: Passenger, dtype: int64
17. Consider the following dataframe df:

employee sales Quarter State


Sahay 125600 1 Delhi
George 235600 1 Tamil
Nadu
Priya 213400 1 Kerala
Manila 189000 1 Haryana
Raina 456000 1 West
Bengal
Manila 172000 2 Haryana
Priya 201400 2 Kerala

i) Write Python Program to create the above dataframe.


ii) Write Python Program to find total sales per state.
iii) Write Python Program to find total sales per employee.
iv) Write Python Program to find maximum sales quarter-wise.

Ans:
import pandas as pd
dx={'employee':['Sahay','George','Priya','Manila','Raina','Manila','Priya'],'Sales':[125600,235600,2134
00,189000,456000,172000,201400],'Quarter':[1,1,1,1,1,2,2],'State':['Delhi','TamilNadu','Kerala','Hary
ana','West Bengal','Haryana','Kerala']}
df=pd.DataFrame(dx)
print(df)
print(df.groupby('State').sum().Sales)
print(df.groupby('employee').sum().Sales)
print(df.groupby('Quarter').max().Sales)

Output:
Quarter Sales State employee
0 1 125600 Delhi Sahay
1 1 235600 TamilNadu George
2 1 213400 Kerala Priya
3 1 189000 Haryana Manila
4 1 456000 West Bengal Raina
5 2 172000 Haryana Manila
6 2 201400 Kerala Priya

State
Delhi 125600
Haryana 361000
Kerala 414800
TamilNadu 235600
West Bengal 456000
Name: Sales, dtype: int64

employee
George 235600
Manila 361000
Priya 414800
Raina 456000
Sahay 125600
Name: Sales, dtype: int64

Quarter
1 456000
2 201400
Name: Sales, dtype: int64
18. Consider a dataframe as follows:
A B C
1 56 71 -13
2 -29 -63 34
3 83 -60 71
Write a Python Code to :
i) Create the above dataframe.
ii) Replace all negative numbers with 0
iii) Count the number of elements which are greater than 50

Ans:
import pandas as pd
L=[[56,71,-13],[-29,-63,34],[83,-60,71]]
df=pd.DataFrame(L,index=[1,2,3],columns=['A','B','C'])
print(df)
df[df<0]=0
print(df)
print('Number of elements > 50:',df[df>50].count().sum())

Output:
A B C
1 56 71 -13
2 -29 -63 34
3 83 -60 71
A B C
1 56 71 0
2 0 0 34
3 83 0 71
Number of elements > 50: 4
19. Consider a dataframe as given below:
X Y Z
1 34 78 19
2 81 23 90

Write Python Code to-


i) Create the above dataframe
ii) Count the number of even numbers and number of odd numbers in the dataframe.

Ans:
import pandas as pd
L=[[34,78,19],[81,23,90]]
df=pd.DataFrame(L,index=[1,2],columns=['X','Y','Z'])
print(df)
print('No of Even Numbers:',df[df%2==0].count().sum())
print('No of Odd Numbers:',df[df%2==1].count().sum())

Output:
X Y Z
1 34 78 19
2 81 23 90

No of Even Numbers: 3
No of Odd Numbers : 3
20. Consider the following dataframe:

i) Write a Python Program to create the above dataframe.


ii) Write a Python Progam to display the details of the students who studying
Accounts.
iii) Write a Python to display the name of the student who got the highest score.

Ans:
import pandas as pd
dx={'Name':['Ravi','Mohit','Rajesh','Roshan','Amitesh','Amit'],'Subject':['Accounts','Economics','Accou
nts','Economics','Accounts','BST'],'Score':[90,89,85,65,89,50],'Grade':['A','B','B','C','B','D']}
df=pd.DataFrame(dx)
print(df)
print('Students studying Accounts')
print(df[df['Subject']=='Accounts'])
print('Student who got the highest')
print(df['Name'][df['Score']==df['Score'].max()])

Output:
Name Subject Score Grade
0 Ravi Accounts 90 A
1 Mohit Economics 89 B
2 Rajesh Accounts 85 B
3 Roshan Economics 65 C
4 Amitesh Accounts 89 B
5 Amit BST 50 D

Students studying Accounts


Name Subject Score Grade
0 Ravi Accounts 90 A
2 Rajesh Accounts 85 B
4 Amitesh Accounts 89 B

Student who got the highest


0 Ravi
Name: Name, dtype: object
PROGRAMS ON MATPLOTLIB
1. Write a Python program to plot a line chart representing the frequency of runs made by a
batsman in successive innings.
Ans:
import matplotlib.pyplot as plt
import numpy as np
runs=[67,67,89,73,73,79,73,91,77,45,91,91,89,89]
runs.sort()
inn=np.arange(14)
plt.plot(inn,runs,'r--')
plt.xlabel('Innings')
plt.ylabel('Runs')
plt.title('Statistical Frequency of Runs')
plt.show()

Output:
2. Write a Python Program to display a bar chart of the number of students in a class. Use
different colors for each bar.
Sample Data:
Class : I, II, III, IV, V, VI, VII, VIII, IX, X
Strength : 40, 43, 45, 47, 49, 38, 50, 37, 43,39
Ans:
import matplotlib.pyplot as plt
clas=('I','II','III','IV','V','VI','VII','VIII','IX','X')
stren=(40,43,45,47,49,38,50,37,43,39)
clr=['red','green','blue','yellow','pink','cyan','magenta','olive','black','lime']
plt.bar(clas,stren,color=clr)
plt.xlabel('Class')
plt.ylabel('Strength')
plt.title('Class Wise Strength')
plt.show()

Output:
3. Write a Python Program to plot a line graph for y=x3
Ans:
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(1,6)
y=x*x*x
plt.plot(x,y,color='red')
plt.xlabel('Values of x')
plt.ylabel('Values of y')
plt.title('Equation')
plt.show()

Output:

4. Write a Python program to create a pie chart with the title of the stream and percentage
of students. Make multiple wedges of the pie.
Sample Data: Science, Commerce, Humanities, Vocational, FMM
Strengths: 29%, 30%, 21%,13%,7%
Ans:
import matplotlib.pyplot as plt
stream=['Science','Commerce','Humanities','Vocational','FMM']
strength=[29,30,21,13,7]
clr=['red','blue','magenta','yellow','pink']
plt.pie(strength,labels=stream,colors=clr)
plt.legend()
plt.show()

Output:
5. Write a Python program to plot a Pie chart of a class of 20 students based on the random
set of marks obtained by the students.
Ans:
import matplotlib.pyplot as plt
classXIIA=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T']
testmarks=[73,82,72,45,61,39,97,74,82,89,92,95,92,72,56,61,78,99,83,41]
plt.pie(testmarks,labels=classXIIA)
plt.title('Class Test Statistics of XII-A')
plt.show()

Output:

-----------------------------

You might also like