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

Class 12 IP Final Practical

Uploaded by

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

Class 12 IP Final Practical

Uploaded by

charul sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

IP Book 12

Practical List
Data Handling
1. Write a code to create a series object by using the Python sequence[4,6,8,10].
Code –
import pandas as pd
s = pd.Series([4,6,8,10])
print(“Series object:”)
print(s)
Output
0 4
1 6
2 8
3 10
dtype: int64

2. Write a code to create a panda’s series from a list of values.

(The list of some values form the series of that values uses list index as
series index.)
Code:
import pandas as pd

lst = ['G','E','E','K','S','F',
'O','R','G','E','E','K','S']
s = pd.Series(lst)
print(s)

Output :
0 G
1 E
2 E
3 K
4 S
5 F
6 O
7 R
8 G
9 E
10 E
11 K
12 S
dtype: object
3. Create a panda's series from a dictionary of values and an ndarray.
import pandas as pd
import numpy as np
dic = {'A':22,'B':32,'C':45,'D':56}
print("Series object is created from Dictionary")
sd = pd.Series(dic)
print("Series")
print(sd)
nd = np.arange(1,10)
print("Series Object by ndarray")
sn = pd.Series(nd)
print(sn)

4. Given a series, print all the elements that are more than 50000.
import pandas as pd
def print_elements_greater_than_50000(series):
for value in series:
if value > 50000:
print(value)
s = pd.Series([10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000,
90000])
print_elements_greater_than_50000(s)

5. Create a Data Frame, quarterly sales, where each row contains the item category, item
name, and expenditure. Group the rows by the category and print the total expenditure per
category.
import pandas as pd
sales_data = { "item_category" : ["food", "drink", "food", "sweet", "food",
"sweet", "drink", "drink"], "item_name" : ["maggi", "pepsi", "rajma rice",
"gulab jamun", "chapati", "rasgulla", "coca cola", "mirinda"],
"expenditure" : [40, 68, 120, 80, 30, 160, 80, 90]}
sales_quart =pd.DataFrame(sales_data)
print(sales_quart)
gdf=sales_quart.groupby("item_category")
new_df=gdf["expenditure"].sum()
print(new_df)

6. Write a Pandas program to perform arithmetic operations on two


Pandas Series.
import pandas as pd
ds1 = pd.Series([3, 6, 9, 12, 15])
ds2 = pd.Series([2, 4, 6, 8, 10])
ds = ds1 + ds2
print("Add two Series:")
print(ds)
print("Subtract two Series:")
ds = ds1 - ds2
print(ds)
print("Multiply two Series:")
ds = ds1 * ds2
print(ds)
print("Divide Series1 by Series2:")
ds = ds1 / ds2
print(ds)

7. # Write a Pandas program to add some data to an existing Series.


import pandas as pd
s = pd.Series(['S101', 'Amjad', 'C.Sc.', 'XII – A1', '450'])
print("Original Data Series:")
print(s)
print("\nData Series after adding some data:")
new_s = s.append(pd.Series(['90.0', 'PASS']))
print(new_s)

8. Write a Pandas program to select the rows where the


percentage greater than 70.

import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'] > 70])

9. Write a Pandas program to select the rows the percentage


is between 70 and 90 (inclusive)

import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']
df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'].between(70,90)])

10. Write a Pandas program to join the two given dataframes


along rows and assign all data.

# Write a Pandas program to join the two given dataframes along rows and
assign all data.
import pandas as pd
import numpy as np
exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit',
'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
exam_data1 = pd.DataFrame(exam_dic1)
exam_dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'],
'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}
exam_data2 = pd.DataFrame(exam_dic2)
1. print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2])
print(result_data)
11. Filter out rows based on different criteria such as duplicate
rows

import pandas as pd
data={'Name':['Aman','Rohit','Deepika','Aman','Deepika','Sohit','Geeta'],
'Sales':[8500,4500,9200,8500,9200,9600,8400]}
sales=pd.DataFrame(data)
# Find duplicate rows
duplicated = sales[sales.duplicated(keep=False)]
print("duplicate Row:\n",duplicated)
12. Importing and exporting data between pandas and CSV file.
# To create and open a data frame using ‘Student_result.csv’ file
using Pandas.
# To display row labels, column labels data types of each column
and the dimensions
# To display the shape (number of rows and columns) of the CSV file.

import pandas as pd
import csv
#Reading the Data
df = pd.read_csv("student_result.csv")
# Display Name of Columns
print(df.columns)
# Display no of rows and column
print(df.shape)
# Display Column Names and their types
print(df.info())

13. Read the ‘Student_result.csv’ to create a data frame and do the


following operation:
# To display Student_result file with new column names.
# To modify the Percentage of student below 40 with NaN value in
dataframe.
import pandas as pd
import numpy as np
import csv
df = pd.read_csv("student_result.csv")
print(df)
#To display Student_result file with new column names.
df1 = pd.read_csv("student_result.csv",skiprows = 1,
names = ['Adno','Sex','Name','Eng','Hin',
'Maths','Sc.','SSt','San','IT','Perc'])
print("To display Student_result file with new column names")
print(df1)
# To modify the Percentage of student below 40 with NaN value.
df2 = pd.read_csv("student_result.csv")
print(df2)
print("To modify the Percentage of student below 40 with NaN value.")
df2.loc[(df2['PERCENTAGE'] <40, 'PERCENTAGE')] = np.nan
print(df2)

14. Read the ‘Student_result.csv’ to create a data frame and do the


following operation:
# To create a duplicate file for ‘student_result.csv’ containing
Adm_No, Name and Percentage.
# Write the statement in Pandas to find the highest percentage and
also print the student’s name and percentage.
import pandas as pd
import numpy as np
import csv
# To create a duplicate file for ‘student_result.csv’ containing Adm_No, Name
and Percentage.
df = pd.read_csv("student_result.csv")
df.to_csv('copyStudent_result.csv',columns=['ADM_NO',"STUDENT'S_NAME
","PERCENTAGE"])
# Display Copied Dataframe
df2=pd.read_csv("copyStudent_result.csv")
print(df2)
# find the highest percentage and also print the student’s name and percentage.
df1 = pd.read_csv("student_result.csv")
df1 = df1[["STUDENT'S_NAME",'PERCENTAGE']]
[df1.PERCENTAGE== df1['PERCENTAGE'].max()]
print(df1)

15 . Locate the 3 largest values in a data frame.


# Locate the 3 largest values in a data frame.
import pandas as pd
data={'Name':['Aman', 'Rohit', 'Deepika', 'Kamal', 'Deva', 'Ramesh',
'Adnan'],
'Sales':[8500, 4500, 9300, 8600, 9200, 9600, 8400]}
sales=pd.DataFrame(data)
# Finding 3 largest value
print(sales.nlargest(3,['Sales']))
16 Write a program to generate a series of 10 numbers with a scalar value of 44.
import pandas as pd
def fl_scv():
print(pd.Series(44,range(1,11)))
fl_scv()
.16. Importing and exporting data between pandas and MySQL
database
import pymysql
import pandas as pd
import mysql.connector
from sqlalchemy import types, create_engine
# Create dataframe
dic={
'EMPNO':[7369,7499,7566,7654,7698,7782,7788,7839,7844,7900,7902,7934],
'ENMAE':['JAMES','ADAMS','CLARK','KING','WARD','JONES','ADAMS','SC
OTT','FORD',
'BLAKE','MARTIN','TURNER'],
'JOB':['CLERK','CLERK','ANALYST','MANAGER','MANAGER','PRESIDEN
T','ANALYST',
'CLERK','MANAGER','ANALYST','SALESMAN','CLERK'],
'MGR':[7876,7876,7782,7900,7900 ,7900,7782,7876,7900,7782,7900,7876],
'HIREDATE':['2005/02/18','2005/01/04','2001/05/18','2003/04/19','2001/07/02',
'2006/09/21','2007/03/13','2005/03/06', '2007/01/12','2009/07/19','2009/01/05',
'2004/11/30'],
'SAL':[11400,19200,29400,60000,15000,95700,13200,36000,36000,34200,1500
0,18000],
'COMM':[4000,5000,5000,4000,2500,4000,2500,3000 ,3000,2500,2000 ,6000],
'DEPTT':[20,30,20,30,30,10,20,10,30,30,20,10]
}
data = pd.DataFrame(dic)
print('Our DataFrame is:\n',data)
tableName="employeedata"
# create sqlalchemy engine
sqlEngine = create_engine("mysql+pymysql://root:@localhost/Company")
dbConnection = sqlEngine.connect()
try:
# Exporting dataframe to SQl
frame = data.to_sql(tableName, dbConnection, if_exists='fail');
except ValueError as vx:
print(vx)
except Exception as ex:
print(ex)
else:
print("Table %s created successfully.\n"%tableName);
finally:
dbConnection.close()

# – Read a MySQL Database Table and write into a Pandas DataFrame:


sqlEngine = create_engine('mysql+pymysql://root:@127.0.0.1')
dbConnection= sqlEngine.connect()
dframe = pd.read_sql("select * from Company.employeedata",
dbConnection);
print("After importing data from MySql:\n")
print(dframe)
dbConnection.close()

17. Replace all negative values in a data frame with a 0.

import pandas as pd
data = {'sales1':[10,20,-4,5,-1,15],
'sales2':[20,15,10,-1,12,-2]}
df = pd.DataFrame(data)
print("Data Frame")
print(df)
print('Display DataFrame after replacing every negative value with 0')
df[df<0]=0
print(df)

18. Create the following DataFrame Sales containing year wise sales figures for five
salespersonsin INR. Use the years as column labels and salesperson names as row labels.

Name/Sales 2018 2019 2020 2021


Megha 110 205 177 189

Karan 130 165 175 198

Dev 115 206 157 179

Radha 118 198 183 169

a. Create the DataFrame.


b. Display the row labels of Sales.
c. Display the column labels of Sales.
d. Display the data types of each column of Sales.
e. Display the dimensions, shape, size and values of Sales.

import pandas as pd
#Creating DataFrame
d = {2018:[110, 130, 115, 118], 2019:[205, 165, 206, 198], 2020:[117, 175,
157, 183], 2021:[189, 198, 179, 169]}
sales=pd.DataFrame(d,index=['Megha','Karan','Dev','Radha'])
#Display row lables
print("Row Lables:\n",sales.index)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
#Display column lables
print("Column Lables:\n",sales.columns)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
#Display data type
print("\nDisplay column data types")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(sales.dtypes)
print("\nDisplay the dimensions, shape, size and values of Sales")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Dimensions:", sales.ndim)
print("Shape:", sales.shape)
print("Size:", sales.size)
print("Values:", sales.values)

19. Consider above dataframe in previous question and write code to do the following:
f. Display the last two rows of Sales.
g. Display the first two columns of Sales.
import pandas as pd
#Creating DataFrame
d = {2018:[110, 130, 115, 118], 2019:[205, 165, 206, 198], 2020:[117, 175,
157, 183], 2021:[189, 198, 179, 169]}
sales=pd.DataFrame(d,index=['Megha', 'Karan', 'Dev', 'Radha'])
print("Display last two rows of DataFrame:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

#Method 1
print("Using tail function:")
print(sales.tail(2))

#Method 2
print("Using iloc")
print(sales.iloc[-2:])

20. Write a panda program to read marks detail of Manasvi and calculate sum of all marks.
data = {'Manasvi': ['Physics', 'Chemistry', 'English', 'Maths', 'Computer Sc'], 'marks':
[89, 99, 97, 99, 98],}
import pandas as pd
import numpy as np
data = {'Manasvi': ['Physics', 'Chemistry', 'English','Maths', 'Computer
Sc'],
'marks': [ 89,99,97,99,98],}
df = pd.DataFrame(data )
print("Sum of Marks:")
print(df['marks'].sum())

21. Write a Pandas program to add, subtract, multiple and divide two Pandas Series.

import pandas as pd
ds1 = pd.Series([2, 4, 6, 8, 10])
ds2 = pd.Series([1, 3, 5, 7, 9])
ds = ds1 + ds2
print("Add two Series:")
print(ds)
print("Subtract two Series:")
ds = ds1 - ds2
print(ds)
print("Multiply two Series:")
ds = ds1 * ds2
print(ds)
print("Divide Series1 by Series2:")
ds = ds1 / ds2
print(ds)

22. Write a program to sort the element of Series S1 into S2.

import pandas as pd
s1 = pd. Series (data = [200, 100, 500, 300, 400], index= ['I', 'K', 'J',
'L', 'M'])
s2 = s1.sort_values()
print("Series object s1:")
print (s1)
print("Series object s2:")
print(s2)

23. Write a program to generate a series of 5 elements with the elements that are multiple of 7.
Start the series with the value 35. Also, multiply the index with 3.

import pandas as pd
import numpy as np
def Ser_mul7():
a = 35
n = np.arange(a,a*2,7)
s = pd.Series(index=n*3,data=n)
print(s)
Ser_mul7()

24. Write a program to generate a series of marks of 10 students. Give grace marks up to 5 of
those who are having <33 marks and print the new list of the marks.

import pandas as pd
def Ser_stumarks():
std_marks = []
for i in range(1,11):
m = int(input("Enter the marks:"))
std_marks.append(m)
s = pd.Series(index=range(1201,1211),data=std_marks)
s[s<33]=s+5
print("New List is:")
print(s[s>=33])
Ser_stumarks()

25. Write a program to generate a series of 10 numbers. Change the value of all the elements to
21 those values are multiples of 4.

import pandas as pd
numbers = []
# Generating a series
for i in range(1,11):
val = int(input("Enter a number :"))
numbers.append(val)
ser = pd.Series(numbers)
# Changing the values of multiple of four and assigning them a value 21
ser[ser % 4 == 0] = 21
print(ser)

26. Create the following Series and do the specified operations:


a. EngAlph, having 26 elements with the alphabets as values and default index values.

import pandas as pd
alph =[chr(i) for i in range(65,91)]
engAlph = pd.Series(alph)
print(engAlph)
b. Vowels, having 5 elements with index labels 'a', 'e', 'i', 'o' and 'u' and all the five values
set to zero. Check if it is an empty series.

import pandas as pd
vowels = pd.Series([1,2,3,4,5],index=list('aeiou'))
print(vowels)
vowels[vowels!='not zero']=0 #
print(vowels)
print()
print('is empty',vowels.empty)

c. Friends, from a dictionary having roll numbers of five of your friends as data and their
first name as keys.

import pandas as pd
di={'data':[1,2,3,4,5],'first name':['Kips','','Python','IP','Book']}
friends = pd.Series(di)
print(friends)

d. MTseries, an empty Series. Check if it is an empty series.

import pandas as pd
MTseries = pd.Series()
print(['Series is not empty','Series is empty'][MTseries.empty])

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
array1 = np.array([31,28,31,30,31,30,31,31,30,31,30,31])
MonthDays = pd.Series(array1,index=[1,2,3,4,5,6,7,8,9,10,11,12])
print(MonthDays)

27. Write a program to generate a series of 10 numbers with scalar value 37.

import pandas as pd
print(pd.Series(37,range(1,11)))

28. Write a program to generate a series and print the top 5 elements using the head function.
import pandas as pd
# generating the series
ser_length = int(input("Enter the length of the series: "))
data = []
for i in range(ser_length):
val = int(input("Enter a val:"))
data.append(val)
ser = pd.Series(data)
print(ser.head(5))
29. Write a program to generate a series and print the bottom 5 elements using the tail
function.

import pandas as pd
# generating the series
ser_length = int(input("Enter the length of the series: "))
data = []
for i in range(ser_length):
val = int(input("Enter a val:"))
data.append(val)
ser = pd.Series(data)
print(ser.tail(5))

30. Create a data frame based on e-commerce data and generate


descriptive statistics (mean, median, mode, quartile, and
variance)
# Create a data frame based on ecommerce data and generate descriptive
statistics # (mean, median,mode, quartile, and variance)
import pandas as pd
sales = {'InvoiceNo': [1001,1002,1903,1004,1085,1006,1007],
'ProductName': ['LCD','AC','Deodrant','leans','Books','Shoes','Jacket'],
'Quantity': [2,1,2,1,2,1,1],
'Price':[65000,55000,500,3000,958,3000,2200]}
df=pd.DataFrame(sales)
print(df)
print("Mean price of Item:", df['Price']. mean ().round (2))
print("Median price of Item:", df['Price']. median ().round (2))
print("Mode of price:\n", df[['Price']]. mode ())
print("Quartile of price:\n",df[['Price']].quantile([.1,.25,.5,.75],axis=0))
print("Variance of Price:\n",df[['Price']].var())
Visualisation

1. Given the school result data, analyses the performance of the students on different
parameters, e.g subject-wise or class-wise.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
marks = { "English" :[67,89,90,55], "Maths":[55,67,45,56],
"IP":[66,78,89,90], "Chemistry" :[45,56,67,65],
"Biology":[54,65,76,87]}
df = pd.DataFrame(marks, index=['Siya', 'Arav', 'Ishi', 'Yash'])
print("******************Marksheet****************")
print(df)
df.plot(kind='bar')
plt.xlabel(" ")
plt.ylabel(" ")
plt.show()
2. For the data frames created above, analyse and
plot appropriate charts with title and legend.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
marks = { "English" :[67,89,90,55], "Maths":[55,67,45,56],
"IP":[66,78,89,90], "Chemistry":[45,56,67,65], "Biology":[54,65,76,87]}
df = pd.DataFrame(marks, index=['Siya', 'Arav', 'Ishi', 'Yash'])
print("******************Marksheet****************")
print(df)
df.plot(kind='bar')
plt.title('Result')
plt.xlabel("marks ")
plt.ylabel("student's name ")
plt.show()

3. Take data of your interest from an open source (e.g. data.gov.in), aggregate and summarise it.
Then plot it using different plotting functions of the Matplotlib library.

CSV: census.csv

Code:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("E:\census.csv")
print(df)
df.plot(kind='bar')
plt.title('Census')
plt.xlabel("")
plt.ylabel("")
plt.show()

4. Observe following data and plot data according to given instructions:

Batsman/Year 2017 2018 2019 2020

Virat Kohli 2501 1855 2203 1223

Steve Smith 2340 2250 2003 1153

Babar Azam 1750 2147 1896 1008

Rohit Sharma 1463 1985 1854 1638

Kane Williamson 1256 1785 1874 1974

Jos Butler 1125 1853 1769 1436

a) Create a bar chart to display data of Virat Kohli & Rohit Sharma.
b) Customise the chart in the following manner:
 Use different widths
 Use different colours to represent different years' score
 Display appropriate titles for axis and chart
 Show legends
c) Create a bar chart to display data of Steve Smith, Kane Williamson & Jos Butler. Customize
Chart as per your wish.
d) Display data of all players for the specific year.

Ans a) and Ans b)


import numpy as np
import matplotlib.pyplot as plt

barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))

Virat_Kohli = [2501,1855,2203,1223]
Rohit_Sharma = [1463,1985,1854,1638]

br1 = np.arange(len(Virat_Kohli))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]

plt.bar(br1, Virat_Kohli, color ='r', width = barWidth,


edgecolor ='grey', label ='Virat_Kohli')
plt.bar(br2, Rohit_Sharma, color ='g', width = barWidth,
edgecolor ='grey', label ='Rohit_Sharma')

plt.xlabel('Year', fontweight ='bold', fontsize = 15)


plt.ylabel('Runs', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(Virat_Kohli))],
[ '2017', '2018', '2019','2020'])

plt.legend()
plt.show()
Ans c)
import numpy as np
import matplotlib.pyplot as plt
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))

Steve_Smith = [2340,2250,2003,1153]
Kane_Williamson = [1256,1785,1874,1974 ]
Jos_Butler = [1125,1853,1769,1436]

br1 = np.arange(len(Steve_Smith))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]

plt.bar(br1, Steve_Smith, color ='r', width = barWidth,


edgecolor ='grey', label ='Steve_Smith')
plt.bar(br2, Kane_Williamson, color ='g', width = barWidth,
edgecolor ='grey', label ='Kane_Williamson')
plt.bar(br3, Jos_Butler, color ='b', width = barWidth,
edgecolor ='grey', label ='Jos_Butler')

plt.xlabel('Years', fontweight ='bold', fontsize = 15)


plt.ylabel('Runs', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(Steve_Smith))],
['2017', '2018', '2019','2020'])

plt.legend()
plt.show()

Ans d)
import numpy as np
import matplotlib.pyplot as plt
barWidth = 0.10
fig = plt.subplots(figsize =(12, 8))

Virat_Kohli = [2501,1855,2203,1223]
Steve_Smith = [2340,2250,2003,1153]
Babar_Azam = [1750,2147,1896,1008]
Rohit_Sharma = [1463,1985,1854,1638]
Kane_Williamson = [1256,1785,1874,1974 ]
Jos_Butler = [1125,1853,1769,1436]

br1 = np.arange(len(Steve_Smith))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
br4 = [x + barWidth for x in br3]
br5 = [x + barWidth for x in br4]
br6 = [x + barWidth for x in br5]

plt.bar(br1, Virat_Kohli, color ='red', width = barWidth,


edgecolor ='grey', label ='Virat_Kohli')
plt.bar(br2, Steve_Smith, color ='cyan', width = barWidth,
edgecolor ='grey', label ='Steve_Smith')
plt.bar(br3,Babar_Azam, color ='magenta', width = barWidth,
edgecolor ='grey', label ='Babar_Azam')
plt.bar(br4, Rohit_Sharma, color ='yellow', width = barWidth,
edgecolor ='grey', label ='Rohit_Sharma')
plt.bar(br5, Kane_Williamson, color ='green', width = barWidth,
edgecolor ='grey', label ='Kane_Williamson')
plt.bar(br6, Jos_Butler, color ='blue', width = barWidth,
edgecolor ='grey', label ='Jos_Butler')

plt.xlabel('Years', fontweight ='bold', fontsize = 15)


plt.ylabel('Runs', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(Steve_Smith))],
[ '2017', '2018', '2019','2020'])

plt.legend()
plt.show()

5. Draw the histogram based on the Production of Wheat in different Years:


Year: 2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018
Production: 4, 6, 7, 15, 24, 2, 19, 5, 16, 4
import pandas as pd
import matplotlib.pyplot as plt
data={'Year':[2000,2002,2004,2006,2008,2010,2012,2014,2016,2018],
'Production':[4,6,7,15,24,2,19,5,16,4]}
d=pd.DataFrame(data)
print(d)
x=d.hist(column='Production',bins=5,grid=True)
plt.show(x)

6. The table shows passenger car fuel rates in miles per gallon for several years. Make a line graph
of the data. During which 2-year period did the fuel rate decrease?

Year: 2000 2002 2004 2006


Rate: 21.0 20.7 21.2 21.6
import matplotlib.pyplot as p
Yr=[2000,2002,2004,2006]
rate=[21.0,20.7,21.2,21.6]
p.plot(Yr,rate)
p.show()

7. The number of bed-sheets manufactured by a factory during five consecutive weeks is given
below.

Week First Second Third Fourth Fifth


Number 600 800 700 800 900
of Bed
sheets

Draw the bar graph representing the above data.


import matplotlib.pyplot as p
x=['First', 'Second', 'Third', 'Fourth', 'Fifth']
y=[600, 850, 700, 300, 900]
p.title('Production by factory')
p.xlabel('week')
p.ylabel('no. of bed sheets')
p.bar(x, y, color='blue', width=.50)
p.show()

8. The number of students in 7 different classes is given below. Represent this data on the bar
graph.

Class 6 7 8 9 10 11 12
Number 100 120 105 110 90 70 65
of
students

import matplotlib.pyplot as p
x=[6,7,8,9,10,11,12]
y=[130,120,135,130,150,80,75]
p.title('class strength')
p.xlabel('class')
p.ylabel('no of students')
p.bar(x, y, color = 'green', width=40)
p.show()

9. Write a program to plot a bar chart in python to display the


result of a school for five consecutive years.
import matplotlib.pyplot as pl
year=['2015','2016','2017','2018','2019'] # list of years
p=[98.50,70.25,55.20,90.5,61.50] #list of pass percentage
j=['b','g','r','m','c'] # color code of bar charts
pl.bar(year, p, width=0.2, color=j) # bar( ) function to create the bar chart
pl.xlabel("year") # label for x-axis
pl.ylabel("Pass%") # label for y-axis
pl.show( ) # function to displa

10.
Data Management

1. Create a student table with the student_id, name, and marks as attributes where the student_id
is the primary key.

Insert the details of a new student in the above table.

Delete the details of a student in the above table.


Use the select command to get the details of the students with marks more than 80.

Find the min, max, sum, and average of the marks in a student marks table.

2. Find the total number of customers from each country in the table (customer ID, customer Name,
country) using group by.

Ans. Select country, count(*) as “total customer” from customer group by country;

3. Write a SQL query to order the (student ID, marks) table in descending order of the marks

Ans. Select student_id, marks from student order by marks desc;

4. Write MySQL Command based on the table Product.

Product Table:

PCODE PNAME PRICE QTY

P01 Pen Ball Point 100 10


P02 Pen Ink 200 5
P03 Pen Gel 150 20
P04 Pencil HB 60 30
P05 Pencil Color 60 25

(i) To display the name of the entire product with price more than 100.

Ans. Select pname from product where price > 100;

(ii) To display the name of all the products by the quantity more than 20

Ans. Select pname from product where qty > 20;

(iii) To add a new row for product with the details :

“P06”, “Eraser”, 50,200

Ans. Insert into product values(“P06”, “Eraser”, 50,200 );

(iv) Display the product with highest price


Ans. Select max(price), pname from product;

(v) What are the total number of records in the table.

Ans. Select count(*) from product;

You might also like