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

Python of data science Practical BE Sem-5

The document outlines a series of practical exercises for learning Python, particularly focused on data science applications. It includes various programming tasks such as arithmetic operations, number conversions, palindrome checks, area calculations, and data structure manipulations using loops and NumPy/Pandas. Each practical exercise is accompanied by the aim, program code, and expected output.

Uploaded by

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

Python of data science Practical BE Sem-5

The document outlines a series of practical exercises for learning Python, particularly focused on data science applications. It includes various programming tasks such as arithmetic operations, number conversions, palindrome checks, area calculations, and data structure manipulations using loops and NumPy/Pandas. Each practical exercise is accompanied by the aim, program code, and expected output.

Uploaded by

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

220490131077 Python for Data Science

Practical Set 1: Basics of python

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 1
220490131077 Python for Data Science

Practical:1
Aim: Write a python program to create a simple arithmetic application including
operations (addition, subtraction, multiplication, division, modulus, exponent, integer
division)

Program:
a= int(input("Enter a number:"))
b= int(input("Enter a number:")
print("The value of arithmetic operations are")
add = a+b
print("Addition is ",add)
sub = a-b
print("Subtraction is ",sub)
mul = a*b
print("Multiplication",mul)
div = a/b
print("Division is",div)
mod = a%b
print("Modulus is ",mod)
exp = (a**b)
print("Exponent is ",exp)
intdiv = (a//b)
print("Integer division is ",intdiv)
Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 2
220490131077 Python for Data Science

Practical: 2
Aim: Write a python program to convert numbers from octal, binary and hexadecimal
systems into decimal number system.

Program:
a = input("Enter the Binary Number:")
print("The decimal number of given binary number is",int(a,2))

b = input("Enter the Octal Number:")


print("The decimal number of given octal number is",int(b,8))

c = input("Enter the Hexadecimal Number:")


print("The decimal number of given hexadecimal number is",int(c,16))

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 3
220490131077 Python for Data Science

Practical: 3
Aim: Write a python program to convert numbers from decimal number system into
octal, binary and hexadecimal system.

Program:
a = int (input("Enter the decimal number you want to convert: "))
print("It's binary number is",int(a))
print("It's octal number is",oct(a))
print("It's hexadecimal number is",hex(a))

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 4
220490131077 Python for Data Science

Practical: 4
Aim: Write a python program to check whether the given number is a palindrome or not.

Program:
a= int(input("Enter a number to check whether it is palindrome or not:"))
sum = 0
temp = a
while(a>0):
r = a%10
sum=(sum*10)+r
a= int(a/10)

if(temp==sum):
print("The number is a palindrome.")

else:
print("The number is not a palindrome.")

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 5
220490131077 Python for Data Science

Practical: 5
Aim: Write a python program to calculate area of a triangle.

Program:
l = float(input("Enter the height of triangle:"))
b = float(input("Enter the base of triangle:"))
area = (l*b)/2
print("The area of trinagle is",area)

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 6
220490131077 Python for Data Science

Practical: 6
Aim: Write a python program to display maximum of given 3 numbers.

Program:
a= int(input("Enter no.1 : "))
b= int(input("Enter no.2 : "))
c= int(input("Enter no.3 : "))
if(a>b and a>c):
print("The maximum of 3 integers is",a)
elif(b>c and b>a):
print("The maximum of 3 integers is",b)
else:
print("The maximum of 3 integers is",c)

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 7
220490131077 Python for Data Science

Practical: 7
Aim: Write a python program to find those numbers which are divisible by 3 and multiple
of 5 within 500 numbers.

Program:
for a in range(0,501):

if(a%3==0 and a%5==0):


print(a,end =' ')

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 8
220490131077 Python for Data Science

Practical: 8
Aim: Write a python program to draw kite pattern using for loop.

Program:
a = int(input("Enter the size for pattern:"))
for x in range(a,0,-1):
print(" "*x,"* "*(a-x))
for x in range(0,a,1):
print(" "*x,"* "*(a-x))
for x in range(a-1,int(a/2)-1,-1):
print(" "*x,"* "*(a-x))

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 9
220490131077 Python for Data Science

Practical Set 2:
Looping and Data Structure with Python

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 10
220490131077 Python for Data Science

Practical: 1
Aim: Write a python program to print numbers from 1 to 50. For multiple of 4 print
name instead of number and for multiple of 5 print father name. For the numbers which
are multiple of both 4 and 5 print surname.

Program:
for i in range(1,21):
b=i
if(i % 4 == 0):
b="Patel"
if(i % 5 == 0):
b="Sneh"
if(i % 4 == 0 and i % 5 ==0):
b="Manishkumar"
print(b)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 11
220490131077 Python for Data Science

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 12
220490131077 Python for Data Science

Practical: 2
Aim: Write a python program to find numbers between 500 and 800 when each digit of number
is ODD and the number should be printed in sequence separated by comma.

Program:
item=[]
for i in range(500,801):
s=str(i)
if(int(s[0])%2!=0) and (int(s[1])%2!=0) and (int(s[2])%2!=0):
item.append(s)
print(", " .join(item))

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 13
220490131077 Python for Data Science

Practical: 3
Aim: Write a python program which accept a sequence of 4 digit binary numbers
separated by comma and also print the numbers which are divisible by 3 in sequence
separated by comma.

Program:
item=[]
print("Enter the 4 bit binary sequence sperated by comma ")
number = [x for x in input().split(",") ]
for p in number:
x=int(p,2)
if (x % 3 ==0):
item.append(p)
print(", " .join(item))

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 14
220490131077 Python for Data Science

Practical: 4
Aim: Write a python program to display Fibonacci sequence up to nth term using
recursive functions.

Program:
def recurr_fib(n) :
if n<=1:
return n
else :
return(recurr_fib(n-1)+recurr_fib(n-2))
num = int(input("Enter the Range of Fib :"))
if num<=0:
print("Enter a positive Number")
else :
for i in range (num) :
print(recurr_fib(i))

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 15
220490131077 Python for Data Science

Practical: 5
Aim: Write a python program that accept a string and calculate the number of uppercase
and lowercase letter.

Program:
str=input("Enter the string to check the lower and upper values \n")
b=0
m=0
for i in str:
if (i<='z' and i>='a'):
b=b+1
if( i>='A' and i<='Z'):
m=m+1
print("THE Lower CASE VLAUES ARE ",b)
print("THE Upper CASE VLAUES ARE ",m)

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 16
220490131077 Python for Data Science

Practical: 6
Aim: : Write a python program to search a number in array using sequential search.

Program:
import numpy as nd
def seq_search(array,num) :
pos=0
found=False
while pos<len(array) and not found:
if (array[pos]==num):
found=True
else:
pos=pos+1
return found,pos+1
array1=nd.random.randint(50,size=(10))
print(array1)
number=int(input("Enter the number you want to search "))
print(seq_search(array1,number) )

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 17
220490131077 Python for Data Science

Practical: 7
Aim: Write a python program to sort elements of array.

Program:
size = int(input("enter size of an array : "))
arr = []
for i in range(size):
element = int(input("enter element in array :"))
arr.append(element)
arr.sort()
print("sorted array in ascending order is : ",arr)
print("sorted array in descending order is : ",arr[::-1])

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 18
220490131077 Python for Data Science

Practical: 8
Aim: Write a python program to input two matrix and perform the following given
operation.

Program:
import numpy
arr1=([1,2,3],[4,5,6],[7,8,9])
arr2=([3,2,1],[6,5,4],[9,8,7])
result=([0,0,0],[0,0,0],[0,0,0])
mat1=numpy.array(arr1)
mat2=numpy.array(arr2)
print("The addition of matrix is \n",mat1+mat2)
print("The Subtraction of matrix is \n",mat1-mat2)
for i in range(0,len(mat1),1):
for k in range(0,len(mat2[0]),1):
for j in range(0,len(mat2),1):
result[i][j] +=mat1[i][k]*mat2[k][j]
print("The multiplication of matrix is ",)
for m in result:
print(m)
print("The Transpose of matrix is \n",mat1.T)
print("The Transpose of matrix is \n",mat2.T)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 19
220490131077 Python for Data Science

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 20
220490131077 Python for Data Science

Practical Set 3:
To study the use of NumPy and Pandas

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 21
220490131077 Python for Data Science

Practical: 1
Aim: Do as directed:
a) Read student data from given excel file sheet named as “5CSE” into appropriate
pandas data structure.
b) Fill missing values in columns “Subject” and “Batch” using forward fill method.
c) Fill value “Jay Patel” in “Mentor” column for students having “Enrollment” column
value from “200860131001” to “200860131029” and “Pal Patel” for remaining students.
d) Add a new column “City” in existing student data and fill that column with
residential city of student.
e) Count total number of students subject-wise and batch-wise.
Program:
a)
import pandas as pd
df = pd.read_csv('5CSE.csv')
df

b)
df.drop("Unnamed: 0",axis=1,inplace=True)
df["Subject"].fillna(method="ffill",inplace=True)
df["Batch"].fillna(method="ffill",inplace=True)
df

c)
def value(x):
if(x<=200860131029):
return "Jay Patel"
else:
return "Pal Patel"
df["Mentor"]=df.apply(lambda x:value(x["Enrollment"]),axis=1)
df

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 22
220490131077 Python for Data Science

d)
df['City']=["Vapi","Silvassa","Vapi","Bhilad","Bhilad","Vapi","Vapi","Vapi","Vapi","Vapi",
"Vapi","
Vapi","Vapi","Vapi","Silvassa","Vapi","Vapi","Vapi","Vapi","Udvada","Vapi","Vapi","Vapi
","Vapi",
"Vapi","Vapi","Silvassa","Pardi","Vapi","Bhilad","Vapi","Vapi","Silvassa","Vapi","Vapi","
Vapi","V
api","Vapi","Vapi","Vapi","Bhilad","Vapi"]
df

e)
df.groupby(["Subject"]).size().reset_index(name='Number of Students')
df.groupby(["Batch"]).size().reset_index(name='Number of Students')

Output:
a)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 23
220490131077 Python for Data Science

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 24
220490131077 Python for Data Science

b)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 25
220490131077 Python for Data Science

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 26
220490131077 Python for Data Science

c)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 27
220490131077 Python for Data Science

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 28
220490131077 Python for Data Science

d)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 29
220490131077 Python for Data Science

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 30
220490131077 Python for Data Science

e)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 31
220490131077 Python for Data Science

Practical: 2
Aim: Do as directed:
a) Read data from given csv file into appropriate pandas data structure. Delete rows
having missing values.
b) Calculate average price of cars having four and six cylinder engines.
c) Find out cheapest and most expensive car details.
d) Find out convertible and sedan car details having maximum engine horsepower.
e) Find average sedan car price
f) Count total number of cars per company.
g) Find each company’s highest car price.

Program:
a)
import pandas as pd
import numpy as np
df=pd.read_csv("Automobile_data_Miss.csv")
df
df.dropna(axis='rows')

b)
a1=df["num-of-doors"]=='four'
a_1=df[a1]["price"].mean()
a2=df["num-of-doors"]=='two'
a_2=df[a2]["price"].mean()
avg=(a_1+a_2)/2
print(avg)

c)
print("Detail of cheapest car")
df[df.price == df.price.min()]
SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 32
220490131077 Python for Data Science

print("Detail of expensive car")


df[df.price == df.price.max()]

d)
df1=df.loc[(df['body-style']=='convertible')]
df1=df1.iloc[df1['horsepower'].argmax()]
df2=df.loc[(df['body-style']=='sedan')]
df2=df2.iloc[df2['horsepower'].argmax()]
print('maximum hoursepower of convertible car is\n\n',df1)
print('\n\nmaximum hoursepower of sedan car is\n\n',df2)

e)
df[df["body-style"]=="sedan"].agg({"price":'mean'})

f)
df['make'].value_counts()

g)
car_manufacturers = df.groupby('make')
price= car_manufacturers["make","price"].max()
price

Output:
a)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 33
220490131077 Python for Data Science

b)

c)

d)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 34
220490131077 Python for Data Science

e)

f)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 35
220490131077 Python for Data Science

g)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 36
220490131077 Python for Data Science

Practical Set 4:
Use of matplotlib and pandas Libraries for Data Analysis
and Visualization.

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 37
220490131077 Python for Data Science

Practical: 1
Aim: Plot gender-wise share of overall voters with legend and suitable labels. (Pie
chart).

Program:
from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd
result_df=pd.read_csv('Votes 2019.csv')
result_df
x=result_df["Female "].sum()
print("Sum of Female voters = ",x)
y=result_df["Male"].sum()
print("Sum of Male voters = ",y)
z=result_df["Other "].sum()
print("Sum of Other voters = ",z)
sizes=[x,y,z]
labels=["Female","Male","Other"]
colors=['palegreen','lightskyblue','red']
explode=(0,0.1,0)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,startangle=90,autopct="%.1f%%",
shado
w=True)
plt.legend(labels,loc=2)
plt.show()

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 38
220490131077 Python for Data Science

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 39
220490131077 Python for Data Science

Practical: 2
Aim: Indian states are divided into six administrative zones: Central, East, North,
Northeast, South and Western. Plot six bar chart into single figure to visualize total voters
with suitable chart title.

Program:
zone=['Southern','Southern','North East','North
East','East','North','Central','Western','Western','Western','Western','North','North','North','East
','South
ern','Southern','Southern','Central','Western','North','North East','North East','North
East','North','East','Southern','North','North','North East','Southern','Southern','North
East','North','North','East']
result_df['Zone']=zone
result_df.head()
result_df.to_csv("Votes_2019.csv")
y=result_df['Zone']
fig=plt.figure()
ax=fig.add_axes([0,3,1,1])
l1=ax.bar(y,result_df['Total Voters'])
ax.set_title("Total Voters vs Zone")
ax.set_ylabel("Total Voters",size=12)
ax.set_xlabel("Administrative Zones",size=12)
plt.show()

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 40
220490131077 Python for Data Science

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 41
220490131077 Python for Data Science

Practical: 3
Aim: Plot zone-wise share of total voters with legend and suitable labels. (Pie chart)

Program:
xx=result_df['Total Voters'].groupby(result_df['Zone']).sum()
print(xx)
sizes=[xx["Central"],xx["East"],xx["North"],xx["North East"],xx["Southern"],xx["Western"]]
labels=["Central","East","North","Northeast","South","West"]
colors=['c','gold','yellow','chartreuse','lightgreen','tomato']
plt.pie(sizes,colors=colors,labels=labels,autopct="%.1f%%")
plt.legend(labels,loc=4)
plt.show()

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 42
220490131077 Python for Data Science

Practical: 4
Aim: Plot horizontal bar chart for states vs total actual votes with suitable labels.

Program:
Program:
fig=plt.figure()
ax=fig.add_axes([0,3,2,3])
ax.barh(y=result_df['State Name'],width=result_df['Total Actual Votes'])
plt.title("States vs Total actual voters")
plt.ylabel("State name")
plt.xlabel("Total Actual Voters")
plt.show()
Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 43
220490131077 Python for Data Science

Practical: 5
Aim: Plot type-wise share (EVM and Postal) with legend and suitable labels for each
administrative zone into single figure. (Pie chart)

Program:
import numpy as np
fig=plt.figure()
ax=fig.subplots(2,3)
xx=result_df['EVM Vote'].groupby(result_df['Zone']).sum()
yy=result_df['Postal Vote'].groupby(result_df['Zone']).sum()
ax[0][0].pie([xx['East'],yy['East']],autopct='%.2f%%')
ax[0][0].set_title('East Zone')
ax[0][1].pie([xx['North'],yy['North']],autopct='%.2f%%')
ax[0][1].set_title('North Zone')
ax[0][2].pie([xx['Western'],yy['Western']],autopct='%.2f%%')
ax[0][2].set_title('Western Zone')
ax[1][0].pie([xx['Southern'],yy['Southern']],autopct='%.2f%%')
ax[1][0].set_title('Southern Zone')
ax[1][1].pie([xx['Central'],yy['Central']],autopct='%.2f%%')
ax[1][1].set_title('Central Zone')
ax[1][2].pie([xx['North East'],yy['North East']],autopct='%.2f%%')
ax[1][2].set_title('Noth-East Zone')
plt.legend(["EVM vote","Postal Votes"])
plt.show()

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 44
220490131077 Python for Data Science

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 45
220490131077 Python for Data Science

Practical: 6
Aim: Plot vote deficits (Total actual votes – Total voters) for each states using line chart.

Program:
result_df.loc[:,"New"]=result_df.loc[:,"Total Actual Votes"].subtract(result_df.loc[:,"Total
Voters"])
result_df
fig = plt.figure()
ax = fig.add_axes([0,3,2,3])
ax.plot(result_df['State Name'],result_df['New'],color='purple',marker='o')
plt.title("State vs Total Actual Votes - Total voters",fontsize=14)
plt.xticks(rotation=90)
plt.xlabel("State name",fontsize=14)
plt.ylabel("Total Actual Votes - Total Voters",fontsize=14)
plt.show()

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 46
220490131077 Python for Data Science

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 47
220490131077 Python for Data Science

Practical: 7
Aim: Plot horizontal bar chart for states vs male, female and others votes (grouping of
bars) with legend and suitable title.

Program:
fig = plt.figure()
ax = fig.add_axes([0,3,2,3])
pos=list(range(len(result_df["Female "])))
width=0.25
ax.barh(pos,result_df['Female '],width,alpha=0.5,color='purple',label=result_df['State
Name'][0])
ax.barh([p + width for p in pos],
result_df['Male'],width,alpha=0.5,color='mediumpurple',label=result_df['State Name'][1])
ax.barh([p + width*2 for p in pos],
result_df['Other '],width,alpha=0.5,color='pink',label=result_df['State Name'][2])
ax.set_title('State vs male, female, other voters')
ax.set_yticks([p + 1.5*width for p in pos])
ax.set_yticklabels(result_df['State Name'])
plt.ylim(min(pos)-width,max(pos)+width*4)
plt.xlim([0,max(result_df['Female ']+result_df['Male']+result_df['Other '])])
plt.legend(['Female','Male','Other '],loc='upper right')
plt.grid()
plt.show()

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 48
220490131077 Python for Data Science

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 49
220490131077 Python for Data Science

Practical Set 5:
To study the sklearn Library and perform various
statistics.

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 50
220490131077 Python for Data Science

Practical: 1
Aim: Load iris dataset from sklearn library given iris.csv file into appropriate data
structure of pandas.

Program:
%matplotlib inline
from sklearn.datasets import load_iris
iris = load_iris()
import pandas as pd
import numpy as np
print('Your pandas version is: %s' % pd. version )
print('Your NumPy version is %s' % np. version )
from sklearn.datasets import load_iris
iris = load_iris()
iris_nparray = iris.data
iris_dataframe = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_dataframe['group'] = pd.Series([iris.target_names[k] for k in iris.target],
dtype="category")
iris_dataframe

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 51
220490131077 Python for Data Science

Practical: 2
Aim: Perform Descriptive Statistics for Numeric Data, Measuring central tendency,
Measuring variance and range.

Program:
a) print(iris_dataframe.mean(numeric_only=True))
b) print(iris_dataframe.median(numeric_only=True))
c) print(iris_dataframe.std())
d) print(iris_dataframe.max(numeric_only=True) - iris_dataframe.min(numeric_only=True))
e) print(iris_dataframe.quantile([0,.25,.50,.75,1]))

Output:
a)

b)

c)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 52
220490131077 Python for Data Science

d)

e)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 53
220490131077 Python for Data Science

Practical: 3
Aim: To Working with percentiles and defining measures of normality.

Program:
a)
from scipy.stats import kurtosis, kurtosistest
variable = iris_dataframe['petal length (cm)']
k = kurtosis(variable)
zscore, pvalue = kurtosistest(variable)
print('Kurtosis %0.3f z-score %0.3f p-value %0.3f' % (k, zscore, pvalue))

b)
from scipy.stats import skew, skewtest
variable = iris_dataframe['petal length (cm)']
s = skew(variable)
zscore, pvalue = skewtest(variable)
print('Skewness %0.3f z-score %0.3f p-value %0.3f' % (s, zscore, pvalue))

Output:
a)

b)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 54
220490131077 Python for Data Science

Practical: 4
Aim: To Counting for Categorical Data, Understanding frequencies, Creating
contingency tables.

Program:
a)
pcts = [0, .25, .5, .75, 1]
iris_binned = pd.concat(
[pd.qcut(iris_dataframe.iloc[:,0], pcts, precision=1),
pd.qcut(iris_dataframe.iloc[:,1], pcts, precision=1),
pd.qcut(iris_dataframe.iloc[:,2], pcts, precision=1),
pd.qcut(iris_dataframe.iloc[:,3], pcts, precision=1)],
join='outer', axis = 1)
print(iris_dataframe['group'].value_counts())

b)
print(iris_binned['petal length (cm)'].value_counts())

c)
print(pd.crosstab(iris_dataframe['group'], iris_binned['petal length (cm)']))

Output:
a)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 55
220490131077 Python for Data Science

b)

c)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 56
220490131077 Python for Data Science

Practical: 5
Aim: To Creating Applied Visualization for EDA like boxplots.

Program:
a)
boxplots = iris_dataframe.boxplot(fontsize=9)

b)
import matplotlib.pyplot as plt
boxplots = iris_dataframe.boxplot(column='petal length (cm)', by='group', fontsize=10)
plt.suptitle("")
plt.show()
Output:

a)

b)

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 57
220490131077 Python for Data Science

Practical Set 6:
Create various plots using matplotlib library.

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 58
220490131077 Python for Data Science

Practical: 1
Aim: Prepare a Pie charts by taking suitable data as reference.

Program:
import matplotlib.pyplot as plt

values = [2, 7, 5, 10, 6, 4]


colors = ['b', 'g', 'r', 'c', 'm', 'y']
labels = ['A', 'B', 'C', 'D', 'E', 'F']
explode = (0, 0.2, 0, 0, 0, 0)
plt.pie(values, colors=colors, labels=labels, explode=explode, autopct='%1.1f%%',
counterclock=False, shadow=True)
plt.title('Values')
plt.show()

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 59
220490131077 Python for Data Science

Practical: 2
Aim: Prepare a Barcharts charts by taking suitable reference.

Program:
import matplotlib.pyplot as plt
values = [4, 9, 3, 10, 5, 8]
widths = [0.7, 0.8, 0.7, 0.7, 0.7, 0.7]
colors = ['b', 'r', 'b', 'b', 'b', 'b']
plt.bar(range(0, 6), values, width=widths, color=colors, align='center')
plt.show()

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 60
220490131077 Python for Data Science

Practical: 3
Aim: Prepare a Histograms by taking suitable reference.

Program:
import numpy as np
import matplotlib.pyplot as plt

x = 20 * np.random.randn(10000)
plt.hist(x, 25, range=(-50, 50), histtype='stepfilled', align='mid', color='g', label='Test Data')
plt.legend()
plt.title('Step Filled Histogram')
plt.show()

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 61
220490131077 Python for Data Science

Practical: 4
Aim: Prepare a Boxplots by taking suitable reference.

Program:
import numpy as np
import matplotlib.pyplot as plt

spread = 100 * np.random.rand(100)


center = np.ones(50) * 50
flier_high = 100 * np.random.rand(10) + 100
flier_low = -100 * np.random.rand(10)
data = np.concatenate((spread, center, flier_high, flier_low))
plt.boxplot(data, sym='gx', widths=.75, notch=True)
plt.show()

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 62
220490131077 Python for Data Science

Practical: 5
Aim: Prepare a Scatterplots by taking suitable reference.

Program:
import numpy as np
import matplotlib.pyplot as plt
x1 = 5 * np.random.rand(40)
x2 = 5 * np.random.rand(40) + 25
x3 = 25 * np.random.rand(20)
x = np.concatenate((x1, x2, x3))
y1 = 5 * np.random.rand(40)
y2 = 5 * np.random.rand(40) + 25
y3 = 25 * np.random.rand(20)
y = np.concatenate((y1, y2, y3))
plt.scatter(x, y, s=[100], marker='^', c='m')
plt.show()

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 63
220490131077 Python for Data Science

Practical: 6
Aim: Prepare a Time Series by taking suitable reference.

Program:
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
start_date = dt.datetime(2022, 10, 20)
end_date = dt.datetime(2022, 11, 5)
daterange = pd.date_range(start_date, end_date)
sales = (np.random.rand(len(daterange)) * 50).astype(int)
df = pd.DataFrame(sales, index=daterange, columns=['Sales'])
df.loc['Oct 4 2022':'Nov 04 2022'].plot()
plt.ylim(0,50)
plt.xlabel('Sales Date')
plt.ylabel('Sale Value')
plt.title('Plotting Time')
plt.show()

Output:

SNPITRC/CSE/20223-24/SEM – 5/3150713
Page | 64

You might also like