Final 1
Final 1
3 Consider the two series objects s11 and s12 that you…
7 Find out the areas that are more than 50000 km²
16 Write a program to plot a bar chart from the medals won by Australia. In the same chart,
plot medals won by India too.
17 Write a program to plot a bar chart from the medals won by the top four countries.
18 Write a program to create a horizontal bar chart from two data sequences
22 QUERIES
23 TABLE GYM
24 TABLE SALESMAN
27 TABLE GARMENTS
1. A Python list, namely section stores the section names ('A', 'B', 'C', 'D') of class
12 in your school. Another list contri stores the contribution made by these
students to a charity fund endorsed by the school. Write code to create a Series
object that stores the contribution amount as the values and the section names as
the indexes.
import pandas as pd
section = ['A', 'B', 'C', 'D']
contri = [6700, 5600, 5000, 5200]
s11 = pd.Series (data = contri, index = section)
print (s11)
Output -
A 6700
B 5600
C 5000
D 5200
dtype: int64
2.Sequences section and contril store the section names ('A', 'B', 'C', 'D', 'E') and
contribution made by them respectively (6700, 5600, 5000, 5200, nil) for a charity.
Your school has decided to donate as much contribution as made by each section,
i.e., the donation will be doubled.
Write code to create a Series object that stores the contribution amount as the
values and the section names as the indexes with datatype as float32.
import pandas as pd
import numpy as np
section ['A', 'B', 'C', 'D', 'E']
contril = np.array([6700, 5600, 5000, 5200, np.NaN])
s12= pd.Series (data = contri12, index = section, dtype = np.float32)
print(s12)
Output -
A 6700.0
B 5600.0
C 5000.0
D 5200.0
E NaN
dtype: float32
3. Consider the two series objects s11 and s12 that you created in examples 11 and 12
respectively. Print the attributes of both these objects in a report form as shown below
import pandas as pd
# statements here to create objects s11 and $12 from previous examples
Output -
4. Consider the Series object s13 that stores the contribution of each section, as
shown below:
A 6700
B 5600
C 5000
D 5200
Write code to modify the amount of section 'A' as 7600 and for sections 'C' and 'D'
as 7000. Print the changed object.
import pandas as pd
s13[0] = 7600
s13[2] 7000
print("Series object after modifying amounts:")
print(s13)
Output -
5. Number of students in classes 11 and 12 in three streams ('Science', 'Commerce'
and 'Humanities') are stored in two Series objects c11 and 12. Write code to find
total number of students in classes 11 and 12, stream wise.
SOLUTION
import pandas as pd
# creating Series objects
c11 = pd.Series (data = [30, 40, 50], index = ['Science', 'Commerce', 'Humanities'])
c12 = pd. Series (data = [37, 44, 45], index = [ 'Science', 'Commerce', 'Humanities'])
# adding two objects to get total no. of students print("Total no. of students")
print (c11+c12) # series objects arithmetic
Output -
Science 67
Commerce 84
Humanities 95
dtype: int64
6. Given a Series that stores the area of some states in km². Write code to find out
the biggest and smallest three areas from the given Series. Given series has been
created like this:
import pandas as pd
Ser1 = pd.Series( [34567, 890, 450, 67892, 34677, 78902, 256711, 678291,
637632, 25723, 2367, 11789, 345, 256517])
print("Top 3 biggest areas are :")
print (Ser1.sort_values().tail(3))
print("3 smallest areas are :")
print(Ser1.sort_values().head (3))
Output -
Top 3 biggest areas are :
6 256711
8 637632
7 678291
dtype: int64
3 smallest areas are :
12 345
2 450
1 890
dtype: int64
7. Find out the areas that are more than 50000 km²
import pandas as pd
Ser1= pd. Series ([34567, 890, 450, 67892, 34677, 78902, 256711, 678291,
637632, 25723, 2367, 11789, 345, 256517])
print (Ser1[Ser1 > 50000])
Output :-
3 67892
5 78902
6 256711
7 678291
8 637632
13 256517
dtype: int64
8. Write a program to create a dataframe from a list containing dictionaries of the
sales performance of four zonal offices. Zone names should be the row labels.
import pandas as pd
zoneA = {'Target' : 56000, 'Sales' : 58000}
zoneB = {'Target' : 70000, 'Sales' : 68000}
zoneC = {'Target' : 75000, 'Sales' : 78000}
zoneD = {'Target' : 60000, 'Sales' : 61000}
sales = [zoneA, zoneB, zoneC, zoneD]
saleDf = pd.DataFrame(sales,
index=['zoneA','zoneB','zoneC','zoneD']) print(saleDf)
Output -
Target Sales
Zone A 56000 58000
Zone B 70000 68000
Zone C 75000 78000
Zone D 60000 61000
9. Write a program to create a dataframe from a list containing 2 lists, each
containing Target and actual Sales figures of four zonal offices. Give appropriate
row labels.
import pandas as pd
Target = [56000, 70000, 75000, 60000]
Sales = [58000, 68000, 78000, 61000]
ZoneSales = [Target, Sales]
zsaleDf = pd.DataFrame (ZoneSales, columns = ['ZoneA', 'ZoneB', 'ZoneC',
'ZoneD'], print(zsaleDf)
Output -
import pandas as pd
Import numpy as np
staff=pd.Series([20,36,44])
salaries=pd.Series([27900,396800,563000])
avg=salaries/staff
org= {'people': staff, 'Amount': salaries, 'Average': avg}
dtf5= pd.DataFrame(org)
print(dtf5)
Output -
11. Given a DataFrame namely aid that stores the aid by NGOs for different states
import pandas as pd
# DataFrame aid created or loaded
print("Aid for books and uniform:")
print(aid[['Books', 'Uniform']])
print("Aid for shoes: ")
print (aid. Shoes)
Output -
Books Uniform
Andhra 8810
Odisha 6798
M.P. 9611
U.P. 6457
Output -
Hospitals Schools
Delhi 189.0 7916.0
Mumbai 208.0 8508.0
Kolkata 149.0 7226.0
Chennai 157.0 7617.0
13. Marks is a list that stores marks of a student in 10 unit tests. Write a program to
plot the student's performance in these 10 unit tests.
ar2 = [1,7,21,35,35,21,7,1]
She wants to plot the sine graph (numpy.sin()), cosine(numpy.cost()) and tangent values
(numpy.tan()) for the same array (ar2)
She wants cyan color for sine plot line, red color for cosine plot line and the black color for
tangent plot line.
SOLUTION -
Output -
15. First 10 terms of a Fibonacci series are stored in a list namely fib:
Write a program to plot Fibonacci terms and their square-roots with two separate lines on
the same plot,
The Fibonacci series should be plotted as a cyan line with 'o' markers having size as 5 and
edge-color as red.
The square-root series should be plotted as a black line with "+markers having size as 7
and edge-color as red.
SOLUTION -
Australia 80 59 59 198
England 45 45 46 136
India 26 20 20 66
Canada 15 40 27 82
New Zealand 15 16 15 46
South Africa 13 11 13 37
Wales 10 12 14 36
Scotland 9 13 22 44
Nigeria 9 9 6 24
Cyprus 8 1 5 14
16. Consider the reference table 3.1.
Write a program to plot a bar chart from the medals won by Australia. In the same
chart, plot medals won by India too.
SOLUTION
OUTPUT -
17. Consider the reference table 3.1. Write a program to plot a bar chart from the
medals won by the top four countries. Make sure that bars are separately visible.
OUTPUT -
19. Prof Awasthi is doing some research in the field of Environment. For some
plotting purposes, he has generated some data as:
import numpy as np
import matplotlib.pyplot as plt
mu = 100
sigma = 15
x = mu + sigma * np.random.randn(10000)
y = mu + 30* np.random.randn(10000)
plt.hist([x,y], bins = 100, histtype = 'barstacked')
plt.title('Research data Histogram')
plt.show()
20. Write a program to read from a CSV file Employee.csv and create a dataframe
from it but dataframe should not use the file's column header rather should use its
own column headings as EmpID, EmpName, Designation and Salary. Also print the
maximum salary given to an employee.
Employee.csv
Empno, Name, Designation, Salary 1001, Trupti, Manager, 56000
1002, Raziya, Manager, 55900
1003, Simran, Analyst, 35000
1004, Silviya, Clerk, 25000
1005, Suji, PR Officer, 31000
SOLUTION
import pandas as pd
edf = pd.read_csv("c:\\pywork\\Employee.csv",\
names['EmpID', 'EmpName', 'Designation', 'Salary'], skiprows = 1)
print(edf)
print("Maximum salary is", edf.Salary.max())
Output -
import pandas as pd
allDf.to_csv("c:\\pywork\\all.csv")
Csv file -
QUERIES -
Q1. Display all the records (all column) from table empl.
SOLUTION:
Q2. Display EmpNo and Ename of all employees from table in empl.
SOLUTION:
Q3. Display Ename, Sal and Sal added with comm from table empl.
SOLUTION:
Q4. Write a query to display employee name, salary and department number who are not
getting commission from table empl.
SOLUTION:
Q5. Write a query to display employee number, name, salary and salary 12 as Annual
Salary whose commission is not NULL from table empl.
SOLUTION:
SOLUTION:
select deptno
from empl;
SOLUTION:
Q8. List details of all clerk who have not been assigned departments as yet.
SOLUTION:
Select*
from empl
where job='clerk'
and deptno IS NULL;
Q9. List the details of those employees who have four lettered names.
SOLUTION:
select*
from empl
where ename like'---';
Q10. List the details of all employees whose annual salary is between 25000-40000.
SOLUTION:
select*
from empl
where sal 12 between 25000
and 40000;
Q12. List the details of employee who earns more Commission than their salaries.
SOLUTION:
select*
from empl
where comm>sal;
Q13. Write a query to display the name, job title and salary of employee who do not have
manager. SOLUTION:
Q14. Write a query to display the name of employee whose name contains 'A' as third
alphabet.
SOLUTION:
Q15. Write a query to display the name of employee whose name contains 'T' as a last
alphabet.
SOLUTION:
Q16. Write a query to display the name of employee who is having 'L'as any alphabet of the
name.
SOLUTION:
select ename from empl where ename like '%L%";
TABLE GYM
SOLUTION:
Q2. To display the names of all the items whose name start with "A".
SOLUTION:
Q3. To display the ICODES and INAMES of all items, whose Brandname is reliable and
Coscore.
SOLUTION:
Q4. To change the brandname to "Fit Trend India" of the item, whose ICODE as "G101".
SOLUTION:
update GYM1
Set BRANDNAME="Fit Trend India"
Where ICODE = "G101";
Q5. Add a new row for new item in gym with the details
SOLUTION:
SALESMAN
Write SQL queries using SQL functions to perform the following operations: (a) Display
salesman name and bonus after rounding off to zero decimal places.
a) To display the salesmen's names and bonuses after rounding them off to zero
decimal places.
b) To display the position of the occurrence of the string 'ta' in the salesmen's
names.
c) To display four characters from the salesmen's names starting from the
second character.
d) To display the month name for the date of joining of the salesmen.
e) To display the name of the weekday for the date of joining of the salesmen.
Given the following table:
(i) It will return error because no argument is passed as decimal places to truncate.
(ii) Output:-
ROUND(AvgMark)
79
73
75
(iii) Output:-
CONCAT (Name,
Stream)
RubinaNonmedical
VikasNonmedical
(iv) Output:-
RIGHT (Stream, 2)
al
ce
ce
es
al
al
es
al
al
ce
Q1. Consider the following table named "SOFTDRINK". Write commands of SQL for (i) to
(iv).
Table: SOFTDRINK
(i) To display names and drink codes of those drinks that have more than 120 calories.
(ii) To display drink codes, names and calories of all drinks, in descending order of calories.
(iii) To display names and price of drinks that have price in the range 12 to 18 (both 12 and
18 included).
(iv) Increase the price of all drinks in the given table by 10%.
Table: GARMENTS