You are on page 1of 60

PRACTICAL FILE

PROGRAM: MBA (BUSINESS ANALYTICS)

SEMESTER-II
ACADEMIC YEAR: 2023-2024

SUBJECT: “INTRODUCTION TO PYTHON”

SUBJECT CODE: KMBA251

SUBMITTED BY: SUBMITTED TO:


THOMAS J GEORGE DR. AJAY TRIPATHI
(Associate Professor)
LIST OF EXPERIMENTS
Course Code: KMBA 251
Course Title: Introduction to Python

SN Name of the Experiment Page No. Signature


1 Start the Python interpreter and use it as a calculator.
 How many seconds are there in 42 minutes 42
seconds? 1
 How many miles are there in 10 kilometers? Hint:
there are 1.61 kilometers in a mile.
 If you run a 10 kilometer race in 42 minutes 42
seconds, What is your average speed in miles per
hour?
2 Write a python program to add two numbers. 1-2

3 Write a python program to swap two numbers 2


4 WAP to print a table of any given number. 2-3

5 WAP to find larger of two number. 3-4

6 WAP to find whether the given number is even or odd 4

7 WAP to enter any character. If the entered character is in


lowercase then convert it into uppercase and if it is an 4-5
uppercase character, then convert it into lowercase.

8 WAP to enter the marks of a student in 4 subjects. 5-6


Calculate total and average and display the division
obtained by the student.
9 Write a program using for loop that loops over a sequence. 6
10 Write a program to count the numbers of characters in the 6-7
given string and store them in a dictionary data structure
11 Write a program to use split and join methods in the
given string and trace a birthday with a dictionary data 7-8
structure.
12 a. Write a program to combine two lists into a dictionary.
b. Write a program to compute the number of characters, 8-9
words, and lines in a file.
13 Write a function dups to find all duplicates in the list. 9
14 Write a function unique to find all the unique elements of 9-10
a list.
15 Write a python program to read and write on CSV File. 10-11
16 Demonstrate Map() with Lambda Functions 11-12
Demonstrate Map() with Tuple
17 Write a python program to demonstrate array 12-13
characteristics using numpy
18 Write a Python program to demonstrate indexing in 13
numpy
19 Write a Python program to demonstrate basic operations 14-15
on single array
20 Write a Python program to demonstrate binary operators 15-16
in Numpy
21 Write a Python program to demonstrate sorting in numpy 16
22 Write a Pandas program to create and display a one-
dimensional array-like object containing an array of data 16-17
using Pandas module
23 Write a Pandas program to add, subtract, multiple and
divide two Pandas Series. 17-18
Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 9]
24 Write a Pandas program to get the powers of an array
values element-wise. Note: First array elements raised to 18-19
powers from second array
Sample data: {'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':
[86,97,96,72,83]}
25 Write a Pandas program to create and display a
DataFrame from a specified dictionary data which has the
index labels.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine',
19-20
'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
26 Write a Pandas program to get the first 3 rows of a given
DataFrame.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 21-22
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
27 Write a Pandas program to select the 'name' and 'score'
columns from the following DataFrame. Sample Python
dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 22-23
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
28 Write a Pandas program to select the rows where the
number of attempts in the examination is greater than 2.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
23-24
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
29 Write a Pandas program to count the number of rows and
columns of a DataFrame.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],
24-25
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
30 Write a Pandas program to sort the DataFrame first by
'name' in descending order, then by 'score' in ascending
order.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 25-26-27
'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no',
'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Values for each column will be:
name : "Suresh", score: 15.5, attempts: 1, qualify: "yes",
label: "k"
31 Write a Pandas program to create a dataframe and set a 27-28
title or name of the index column.
32 Write a Pandas program to join the two given dataframes 28-29
along rows and assign all data.
33 Write a Pandas program to append rows to an existing 29-30
DataFrame and display the combined data.
34 Write a Pandas program to append a list of dictioneries or 30-31-31
series to a existing DataFrame and display the combined
data.
35 Write a Pandas program to create
a) Datetime object for Jan 15 2012.
b) Specific date and time of 9:20 pm.
c) Local date and time. 32-33
d) A date without time.
e) Current date.
f) Time from a datetime.
g) Current local time.
36 Write a Pandas program to create
a) a specific date using timestamp.
b) date and time using timestamp. 33-34
c) a time adds in the current local date using timestamp.
d) current date and time using timestamp.
37 Write a Pandas program to create a date from a given
year, month, day and another date from a given string 35
formats.
38 Write a Pandas program to print the day after and before a
specified date. Also print the days between two given 36-37
dates.
39 Write a Pandas program to create the todays date 37
40 Write a Pandas program to get the current date, oldest
date and number of days between Current date and oldest 37-38
date of Ufo dataset.
41 Write a Pandas program to convert given datetime to 38-39
timestamp.
42 Write a Pandas program to create a line plot of the
historical stock prices of Alphabet Inc. between two 39-40
specific dates
43 Write a Pandas program to create a bar plot of the trading 40-42
volume of Alphabet Inc. stock between two specific dates.
44 Write a Pandas program to create a histograms plot of 42-43
opening, closing, high, low stock prices of Alphabet Inc.
between two specific dates.
45 Write a python program to demonstrate use of matplotlib
to display bar graph, histogram, scatter plot, area plot, pie 44-47
plot.
46 Write a python program to demonstrate Natural Language 47-49
Processing
47 Write a python program to demonstrate image processing 50-51
48 Write a python program for K Nearest Neighbors 51-54
Algorithm for Classification
1

1.Start the Python interpreter and use it as a calculator.


 How many seconds are there in 42 minutes 42 seconds?
 How many miles are there in 10 kilometers? Hint: there are 1.61 kilometers in a mile.
If you run a 10 kilometer race in 42 minutes 42 seconds, What is your average speed in
miles per hour?

Time_min = 42
Time_sec = 42
New_Time_sec = Time_min * 60 + Time_sec
print(New_Time_sec)
OUTPUT:

2.Write a python program to add two numbers.

num1 = int(input("Enter Number 1 : "))


num2 = int(input("Enter Number 2 : "))
result = num1 + num2
print("{0} + {1} = {2}".format(num1,num2,result))
OUTPUT:

2.Write a python program to multiple two numbers.


CODE:
num1 = int(input("Enter Number 1 : "))
num2 = int(input("Enter Number 2 : "))
Result = num1 * num2
print("{0} * {1} = {2}".format (num1,num2,result))
OUTPUT:
2

3.Write a python program to swap two numbers

num1 = int(input("Enter Number 1 : "))


num2 = int(input("Enter Number 2 : "))
print("Before swapping num1 = {0} num2 = {1}".format(num1,num2))
temp = num1
num1 = num2
num2 = temp
print("After swapping num1 = {0} num2 = {1}".format(num1,num2))
OUTPUT:

4.WAP to print a table of any given number.


CODE:
#For Loop:-
n=int(input("Enter any number"))
for i in range(1,11):
print(n,"x",i,"=",n*i)
OUTPUT:
3

#While Loop:-
n=int(input("Enter any number"))
i=1
while i<11:
print(n,"x",i,"=",n*i)
i=i+1
OUTPUT:

5.WAP to find larger of two number.


CODE:
n1=int(input("Enter the 1 number"))
n2=int(input("Enter the 2 number"))
if n1>n2:
print(n1,"is the largest number")
else:
print(n2,"is the largest number")
4

OUTPUT:

6.WAP to find whether the given number is even or odd


CODE:
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
OUTPUT:

7.WAP to enter any character. If the entered character is in lowercase then


convert it into uppercase and if it is an uppercase character, then convert it into
lowercase.
CODE:
# take input from user
char = input("Enter a character: ")

# check if the character is lowercase


if char.islower():
# convert to uppercase
char = char.upper()
print("The uppercase character is:", char)
# check if the character is uppercase
elif char.isupper():
# convert to lowercase
5

char = char.lower()
print("The lowercase character is:", char)
else:
print("Invalid input!")
OUTPUT:

8. WAP to enter the marks of a student in 4 subjects. Calculate total and average
and display the division obtained by the student.
CODE:
a=int(input("Enter physics marks:\n"))
b=int(input("Enter hindi marks:\n"))
c=int(input("Enter english marks:\n"))
d=int(input("Enter math marks:\n"))
total=a+b+c+d
print("The total of marks is "+str(total))
avg=a+b+c+d
print("The total of marks is "+str(avg))
percent=(total/400)*100
if(percent>=60):
print("First Division")
elif(percent>50 and percent<59.9):
print("Second division")
elif(percent>33 and percent<49.9):
print("Third Division")
else:
print("pass")
OUTPUT:
6

9.Write a program in python for a user define function diff which is use to
function diff num?
CODE:
def diff(num1, num2):
# calculate the absolute difference between num1 and num2
diff = abs(num1 - num2)
# return the result
return diff

# example usage
result = diff(10, 7)
print("The absolute difference between 10 and 7 is:", result)
OUTPUT:

10.Write a program to count the numbers of characters in the given string and store them in a
dictionary data structure?
CODE:# take input string from user
string = input("Enter a string: ")
# initialize an empty dictionary
char_count = {}
7

# iterate over the characters in the string


for char in string:
# if the character is already in the dictionary, increment its count
if char in char_count:
char_count[char] += 1
# if the character is not in the dictionary, add it with count 1
else:
char_count[char] = 1
# print the character counts
for char, count in char_count.items():
print(f"{char}: {count}")
OUTPUT:

11.Write a program to use split and join methods in the given string and trace a birthday
with a dictionary data structure.
CODE:
# take input string from user
string = input("Enter a name and birthday (MM/DD/YYYY) separated by a space: ")
# split the string into name and birthday
name, birthday = string.split()
# create a dictionary with the name and birthday
birthday_dict = {name: birthday}
# print the dictionary
8

print(birthday_dict)
# convert the dictionary to a string using join
birthday_str = " ".join([f"{name}: {birthday}" for name, birthday in birthday_dict.items()])
# print the string
print(birthday_str)
OUTPUT:

12. a. Write a program to combine two lists into a dictionary.


b. Write a program to compute the number of characters, words, and lines in a file.
CODE:
dat1 = ["Name","Age","City"]
dat2 = ["Andy",6,"Ohayo"]
my_dict = dict(zip(dat1,dat2))
print(my_dict)
OUTPUT:

b.
9

13. Write a function dups to find all duplicates in the list.


CODE:
def dups(lst):
"""
Find all duplicates in a list.
"""
seen = set()
dups = set()
for item in lst:
if item in seen:
dups.add(item)
else:
seen.add(item)
return list(dups)
my_list = [1, 2, 3, 2, 4, 3, 5, 6, 5]
print(dups(my_list))
OUTPUT:

14. Write a function unique to find all the unique elements of a list.
CODE:
def unique(lst):
"""
Find all unique elements in a list.
"""
seen = set()
unique_items = [ ]
10

for item in lst:


if item not in seen:
seen.add(item)
unique_items.append(item)
return unique_items
my_list = [1, 2, 4, 5, 6, 6, 7, 7, 2, 8, 9, 10]
print(unique(my_list))
OUTPUT:

15.Write a python program to read and write on CSV File.


CODE:
import csv

# Open CSV file for writing

with open('my_data.csv', mode='w', newline='') as file:

writer = csv.writer(file)

# Write header row

writer.writerow(['Name', 'Age', 'Country'])

# Write data rows

writer.writerow(['Alice', 25, 'USA'])

writer.writerow(['Bob', 30, 'Canada'])

writer.writerow(['Charlie', 20, 'Australia'])

# Open CSV file for reading

with open('my_data.csv', mode='r') as file:


11

reader = csv.reader(file)

# Read header row

header = next(reader)

# Print header

print(header)

# Read and print data rows

for row in reader:

print(row)

OUTPUT:

16.a.Demonstrate Map() with Lambda Functions.

b.Demonstrate Map() with Tuple.


CODE:

numbers = [1, 2, 3, 4, 5]

# Define lambda function to square a number

square = lambda x: x**2

# Use map() with lambda function

squares = map(square, numbers)

# Convert map object to list and print

print(list(squares))

OUTPUT:
12

CODE: b.

# Define list of tuples

tuples = [(1, 2), (3, 4), (5, 6)]

# Define lambda function to sum the elements of a tuple

sum_tuple = lambda x: x[0] + x[1]

# Use map() with lambda function

sums = map(sum_tuple, tuples)

# Convert map object to list and print

print(list(sums))

OUTPUT:

17.Write a python program to demonstrate array characteristics using numpy.


CODE:
import numpy as np
arr = np.arange(1,11)
print("Array:","arr")
print("Number of dimension:",arr.ndim)
print("Shape:",arr.shape)
print("Size:",arr.size)
print("Data type:",arr.dtype)
print("Maximum value:",arr.max())
print("Minimum value:",arr.min())
print("Sum:",arr.sum())
print("Average:",arr.mean())
OUTPUT:
13

18. Write a Python program to demonstrate indexing in NumPy.


CODE:
# Python program to demonstrate
# the use of index arrays.
import numpy as np

# Create a sequence of integers from


# 10 to 1 with a step of -2
a = np.arange(10, 1, -2)
print("\n A sequential array with a negative step: \n",a)

# Indexes are specified inside the np.array method.


newarr = a[np.array([3, 1, 2 ])]
print("\n Elements at these indices are:\n",newarr)

OUTPUT:
14

19. Write a Python program to demonstrate basic operations on single array.


CODE:
import numpy as np
# create a numpy array
arr=np.array([1,2,3,4,5])
# basics operations on the array
print("original array:",arr)
# Addition
addition = arr + 2
print("Addition:",addition)
# Subtraction
subtraction = arr - 2
print("Subtraction array:",subtraction)
# Multiplication
multiplication = arr * 2
print("Multiplication:",multiplication)
# Division
division = arr / 2
print("Division:",division)
# Exponentiation
exponentiation = arr ** 2
print("Exponentiation:",exponentiation)
# Square root
sqrt = np.sqrt(arr)
print("Square root:", sqrt)
# Sum of all elements
sum_of_elements = np.sum(arr)
15

print("Sum_of_elements:",sum_of_elements)

# Minimum and Maximum


minimum = np.min(arr)
maximum = np.max(arr)
print("Minimum:",minimum)
print("Maximum:",maximum)

# Mean and Standard Deviation


mean = np.mean(arr)
std_dev = np.std(arr)
print("Mean:",mean)
print("Standard deviation:",std_dev)
OUTPUT:

20. Write a Python program to demonstrate binary operators in Numpy.


CODE:
# Python program explaining
# bitwise_and() function

import numpy as np
in_num1 = 10
in_num2 = 11
16

print ("Input number1 : ", in_num1)


print ("Input number2 : ", in_num2)

out_num = np.bitwise_and(in_num1, in_num2)


print ("bitwise_and of 10 and 11 : ", out_num)

OUTPUT:

21. Write a Python program to demonstrate sorting in numpy.


CODE:
# Sort the array
import numpy as np
arr = np.array([3, 2, 0, 1])
arr1 = np.array(['banana', 'cherry', 'apple'])
print(np.sort(arr))
print(np.sort(arr1))

# Sort a 2-D array


arr2 = np.array([[3, 2, 4], [5, 0, 1]])
print(np.sort(arr2))
OUTPUT:

22. Write a Pandas program to create and display a one-dimensional array-like object
containing an array of data using Pandas module.
CODE:
import pandas as pd
17

# Create an array of data


data = [10, 20, 30, 40, 50]
# Create a Series using Pandas
series = pd.Series(data)
# Display the Series
print(series)
OUTPUT:

23. Write a Pandas program to add, subtract, multiple and divide two Pandas Series.
Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 9].
CODE:
import pandas as pd
# Create two Series
series1 = pd.Series([2, 4, 6, 8, 10])
series2 = pd.Series([1, 3, 5, 7, 9])

# Perform arithmetic operations on the Series


addition = series1 + series2
subtraction = series1 - series2
multiplication = series1 * series2
division = series1 / series2

# Display the results


print("Series 1:", series1)
18

print("Series 2:", series2)


print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
OUTPUT:

24. Write a Pandas program to get the powers of an array values element-wise. Note: First
array elements raised to powers from second array
Sample data: {'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]}
CODE:

import pandas as pd

# Create a dictionary with the sample data


19

data = {'X': [78, 85, 96, 80, 86],

'Y': [84, 94, 89, 83, 86],

'Z': [86, 97, 96, 72, 83]}

# Create a DataFrame using the dictionary

df = pd.DataFrame(data)

# Calculate the powers element-wise

powers = df['X'] ** df['Y']

# Display the DataFrame with the powers

print(df)

print("Powers:",powers)

OUTPUT:

25. Write a Pandas program to create and display a DataFrame from a specified dictionary
data which has the index labels.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew',
'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'].
20

CODE:
import pandas as pd
import numpy as np

# Define the dictionary data


exam_data = {
'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']
}
# Define the index labels
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

# Create a DataFrame from the dictionary with index labels


df = pd.DataFrame(exam_data, index=labels)
# Display the DataFrame
print(df)
OUTPUT:
21

26. Write a Pandas program to get the first 3 rows of a given DataFrame.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew',
'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
CODE:
import pandas as pd
import numpy as np

# Define the dictionary data


exam_data = {
'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']
}
# Define the index labels
labels = ['a','b','c','d','e','f','g','h','i','j']
# Create a DataFrame from the dictionary with index labels
df = pd.DataFrame(exam_data, index = labels)
# Get the first 3 rows of the DataFrame
first_three_rows = df.head(3)
# Display the first 3 rows
print(first_three_rows)
OUTPUT:
22

27. Write a Pandas program to select the 'name' and 'score' columns from the following
DataFrame. Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew',
'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
CODE:
import pandas as pd
import numpy as np

# Define the dictionary data


exam_data = {
'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']
}

# Define the index labels


labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

# Create a DataFrame from the dictionary with index labels


df = pd.DataFrame(exam_data, index=labels)
# Select the 'name' and 'score' columns from the DataFrame
23

selected_columns = df[['name', 'score']]


print(selected_columns)
OUTPUT:

28. Write a Pandas program to select the rows where the number of attempts in the
examination is greater than 2.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew',
'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
CODE:
import pandas as pd
import numpy as np

# Define the dictionary data


exam_data = {
'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']
24

# Define the index labels


labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

# Create a DataFrame from the dictionary with index labels


df = pd.DataFrame(exam_data, index=labels)
# Select the rows where the number of attempts is greater than 2
selected_rows = df[df['attempts'] > 2]
print(selected_rows)
OUTPUT:

29. Write a Pandas program to count the number of rows and columns of a DataFrame.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew',
'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
CODE:
import pandas as pd
import numpy as np

# Define the dictionary data


exam_data = {
25

'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']
}

# Define the index labels


labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

# Create a DataFrame from the dictionary with index labels


df = pd.DataFrame(exam_data, index=labels)
# Count the num of rows and columns
num_rows = len(df)
num_cols = len(df.columns)

# Display the num of rows and columns


print("Number of rows:",num_rows)
print("Number of rows:",num_cols)

OUTPUT:

30. Write a Pandas program to sort the DataFrame first by 'name' in descending order, then
by 'score' in ascending order.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew',
'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
26

'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],


'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Values for each column will be:
name : "Suresh", score: 15.5, attempts: 1, qualify: "yes", label: "k"
CODE:
import pandas as pd
import numpy as np

# Define the dictionary data


exam_data = {
'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas', 'Suresh'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19, 15.5],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes', 'yes']
}

# Define the index labels


labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# Create a DataFrame from the dictionary with index labels


df = pd.DataFrame(exam_data, index=labels)

# Sort the DataFrame by 'name' in descending order and 'score' in ascending order
sorted_df = df.sort_values(by=['name', 'score'], ascending=[False, True])

# Display the sorted DataFrame


print(sorted_df)
27

OUTPUT:

31. Write a Pandas program to create a dataframe and set a title or name of the index
column.
CODE:
import pandas as pd

# Create a DataFrame
data = {
'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']
}
df = pd.DataFrame(data)

# Set the name of the index column


df = df.rename_axis('Index Title')

# Display the DataFrame


print(df)
OUTPUT:
28

32. Write a Pandas program to join the two given dataframes along rows and assign all data.
CODE:
import pandas as pd
# Create the first DataFrame
data1 = {
'Name' : ['ana', 'andy', 'samara'],
'Age' : [20, 22, 34],
'City' : ['Newyork', 'London', 'Paris']
}
df1 = pd.DataFrame(data1)
data2 = {
'Name' : ['Akanksha', 'Dhruva', 'Rajeev'],
'Age' : [15, 20, 25],
'City' : ['chicago', 'Berlin', 'Tokyo']
}
df2 = pd.DataFrame(data2)
# Join the two DataFrames along rows and assign all data
joined_df = pd.concat([ df1, df2 ])
# Display the joined DataFrame
print(joined_df)
OUTPUT:
29

33. Write a Pandas program to append rows to an existing DataFrame and display the
combined data.
CODE:
import pandas as pd
# Create the first DataFrame
data1 = {
'Name' : ['ana', 'andy', 'samara'],
'Age' : [20, 22, 34],
'City' : ['Newyork', 'London', 'Paris']
}
df1 = pd.DataFrame(data1)
data2 = {
'Name' : ['Akanksha', 'Dhruva', 'Rajeev'],
'Age' : [15, 20, 25],
'City' : ['chicago', 'Berlin', 'Tokyo']
}
df2 = pd.DataFrame(data2)
# Append the new data to the existing DataFrame
combined_df = df.append(df2)
# Display the joined DataFrame
print(combined_df)
OUTPUT:
30

34. Write a Pandas program to append a list of dictioneries or series to a existing DataFrame
and display the combined data.
CODE:
import pandas as pd

# Create the initial DataFrame


data = {
'Name': ['John', 'Alice'],
'Age': [25, 30],
'City': ['New York', 'London']
}
df = pd.DataFrame(data)

# Create a list of dictionaries or Series to append


new_data = [
{'Name': 'Bob', 'Age': 35, 'City': 'Paris'},
{'Name': 'Emma', 'Age': 28, 'City': 'Chicago'}
]

# Append the new data to the existing DataFrame using append


for data in new_data:
df = df.append(data, ignore_index=True)
31

# Display the combined data


print(df) import pandas as pd

# Create the initial DataFrame


data = {
'Name': ['John', 'Alice'],
'Age': [25, 30],
'City': ['New York', 'London']
}
df = pd.DataFrame(data)

# Create a list of dictionaries or Series to append


new_data = [
{'Name': 'Bob', 'Age': 35, 'City': 'Paris'},
{'Name': 'Emma', 'Age': 28, 'City': 'Chicago'}
]

# Append the new data to the existing DataFrame using append


for data in new_data:
df = df.append(data, ignore_index=True)

# Display the combined data


print(df)
OUTPUT:
32

35. Write a Pandas program to create


a) Datetime object for Jan 15 2012.
b) Specific date and time of 9:20 pm.
c) Local date and time.
d) A date without time.
e) Current date.
f) Time from a datetime.
g) Current local time.
CODE:
import pandas as pd

# a) Datetime object for Jan 15 2012


dt1 = pd.to_datetime('2012-01-15')

# b) Specific date and time of 9:20 pm


dt2 = pd.to_datetime('2012-01-15 21:20:00')

# c) Local date and time


dt3 = pd.to_datetime('now')

# d) A date without time


dt4 = pd.to_datetime('today').date()

# e) Current date
33

current_date = pd.to_datetime('today').date()

# f) Time from a datetime


time_from_datetime = pd.to_datetime('now').time()

# g) Current local time


current_local_time = pd.to_datetime('now').time()

# Display the datetime objects


print("Datetime object for Jan 15 2012:", dt1)
print("Specific date and time of 9:20 pm:", dt2)
print("Local date and time:", dt3)
print("A date without time:", dt4)
print("Current date:", current_date)
print("Time from a datetime:", time_from_datetime)
print("Current local time:", current_local_time)
OUTPUT:

36. Write a Pandas program to create


a) a specific date using timestamp.
b) date and time using timestamp.
c) a time adds in the current local date using timestamp.
d) current date and time using timestamp.
CODE:
import pandas as pd
34

# a) Specific date using timestamp


specific_date = pd.Timestamp(year=2022, month=3, day=12)

# b) Date and time using timestamp


date_and_time = pd.Timestamp(year=2022, month=3, day=12, hour=9, minute=20, second=0)

# c) Time added to the current local date using timestamp


current_local_date = pd.Timestamp('today').normalize()
time_added = pd.Timestamp(year=current_local_date.year, month=current_local_date.month,
day=current_local_date.day, hour=12, minute=30, second=0)

# d) Current date and time using timestamp


current_date_and_time = pd.Timestamp('now')

# Display the datetime objects


print("Specific date:", specific_date)
print("Date and time:", date_and_time)
print("Time added to the current local date:", time_added)
print("Current date and time:", current_date_and_time)
OUTPUT:
35

37. Write a Pandas program to create a date from a given year, month, day and another date
from a given string formats.
CODE:
import pandas as pd
from datetime import datetime

# Function to create a date from given year, month, and day


def create_date(year, month, day):
return pd.to_datetime(f"{year}-{month:02d}-{day:02d}")

# Function to create a date from a given string format


def create_date_from_string(date_string, date_format):
return datetime.strptime(date_string, date_format)
year = 2023
month = 7
day = 20
date_from_parts = create_date(year, month, day)
print("Date from year, month, and day:", date_from_parts)
date_string = "2023-07-20"
date_format = "%Y-%m-%d"
date_from_string = create_date_from_string(date_string, date_format)
print("Date from string format:", date_from_string)

OUTPUT
36

38. Write a Pandas program to print the day after and before a specified date. Also print the
days between two given dates.
CODE:
import pandas as pd

# Specify the date


specified_date = pd.to_datetime('2022-06-15')

# Compute the day after the specified date


day_after = specified_date + pd.DateOffset(days=1)

# Compute the day before the specified date


day_before = specified_date - pd.DateOffset(days=1)

# Specify the start and end dates for the range


start_date = pd.to_datetime('2022-06-10')
end_date = pd.to_datetime('2022-06-20')

# Compute the days between the start and end dates


days_between = pd.date_range(start=start_date, end=end_date) - pd.DateOffset(days=1)

# Print the results


print("Day after specified date:", day_after.date())
print("Day before specified date:", day_before.date())
print("Days between start and end dates:")
for day in days_between:
print(day.date())
37

OUTPUT:

39. Write a Pandas program to create the todays date.


CODE:
import pandas as pd

# Create today's date


today = pd.Timestamp.today().date()

# Print today's date


print("Today's date:", today)
OUTPUT:

40. Write a Pandas program to get the current date, oldest date and number of days between
Current date and oldest date of Ufo dataset.
CODE:-
Code:
import pandas as pd
# Sample data for demonstration
38

data = {
'Date': pd.to_datetime(['2023-07-21', '2022-12-15', '2020-04-10', '2019-08-25'])
}
ufo_df = pd.DataFrame(data)
# Get the current date
current_date = pd.to_datetime('today').date()
# Get the oldest date in the 'Date' column of the DataFrame
oldest_date = ufo_df['Date'].min().date()
# Calculate the number of days between the current date and the oldest date
days_difference = (current_date - oldest_date).days
print("Current Date:", current_date)
print("Oldest Date:", oldest_date)
print("Number of Days between Current Date and Oldest Date:", days_difference)
OUTPUT:-

41. Write a Pandas program to convert given datetime to timestamp.


CODE
import pandas as pd

# Given datetime string


datetime_str = '2023-07-20 12:34:56'

# Convert the datetime string to a Pandas Timestamp object


timestamp = pd.to_datetime(datetime_str)
39

# Extract the timestamp


timestamp_value = timestamp.timestamp()

print("Given Datetime:", datetime_str)


print("Timestamp:", timestamp_value)

OUTPUT

42. Write a Pandas program to create a line plot of the historical stock prices of Alphabet Inc.
between two specific dates
CODE:-
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt

def plot_stock_prices(ticker, start_date, end_date):


# Fetch the stock data using yfinance
stock_data = yf.download(ticker, start=start_date, end=end_date)

# Create the line plot


plt.figure(figsize=(12, 6))
plt.plot(stock_data['Close'], label='Closing Price', color='blue')
plt.plot(stock_data['Open'], label='Opening Price', color='green')
plt.plot(stock_data['High'], label='High Price', color='red')
plt.plot(stock_data['Low'], label='Low Price', color='orange')
40

plt.title(f"Historical Stock Prices for {ticker} between {start_date} and {end_date}")


plt.xlabel('Date')
plt.ylabel('Stock Price')
plt.legend()
plt.grid(True)
plt.show()
# Specify the date range
start_date = '2023-01-01'
end_date = '2023-07-01'
# Call the function with the stock symbol of Alphabet Inc. (GOOGL)
plot_stock_prices('GOOGL', start_date, end_date)
OUTPUT:-

43.Write a Pandas program to create a bar plot of the trading volume of Alphabet Inc. stock
between two specific dates
CODE:-
pip install yfinance
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf
41

def plot_trading_volume(symbol, start_date, end_date):


try:
# Download historical stock prices using Yahoo Finance API
data = yf.download(symbol, start=start_date, end=end_date)

# Create a bar plot for trading volume


plt.figure(figsize=(10, 6))
plt.bar(data.index, data['Volume'], color='b', label='Trading Volume')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.title(f"Trading Volume of {symbol} between {start_date} and {end_date}")
plt.legend()
plt.grid(True)
plt.show()

except Exception as e:
print("Error:", e)

if __name__ == "__main__":
symbol = "GOOG" # Stock symbol for Alphabet Inc.
start_date = "2023-01-01"
end_date = "2023-07-20"
plot_trading_volume(symbol, start_date, end_date)
OUTPUT:-
42

44. Write a Pandas program to create a histograms plot of opening, closing, high, low stock
prices of Alphabet Inc. between two specific dates.
CODE:-
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt

def plot_stock_histogram(ticker, start_date, end_date):


# Fetch the stock data using yfinance
stock_data = yf.download(ticker, start=start_date, end=end_date)

# Create the histogram plot


plt.figure(figsize=(10, 6))
plt.hist(stock_data['Open'], bins=20, alpha=0.7, label='Opening Price')
43

plt.hist(stock_data['Close'], bins=20, alpha=0.7, label='Closing Price')


plt.hist(stock_data['High'], bins=20, alpha=0.7, label='High Price')
plt.hist(stock_data['Low'], bins=20, alpha=0.7, label='Low Price')

plt.title(f"Stock Price Histogram for {ticker} between {start_date} and {end_date}")


plt.xlabel('Stock Price')
plt.ylabel('Frequency')
plt.legend()
plt.grid(True)
plt.show()

# Specify the date range


start_date = '2023-01-01'
end_date = '2023-07-01'

# Call the function with the stock symbol of Alphabet Inc. (GOOGL)
plot_stock_histogram('GOOGL', start_date, end_date)
OUTPUT:-
44

45. Write a python program to demonstrate use of matplotlib to display bar graph,
histogram, scatter plot, area plot, pie plot.
CODE
import matplotlib.pyplot as plt

# Sample data for demonstration


x = [1, 2, 3, 4, 5]
y = [5, 10, 15, 20, 25]
data = [10, 15, 30, 25, 20]

# Bar Graph
plt.figure(figsize=(8, 4))
plt.bar(x, y, label='Bar Graph')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Bar Graph Example')
plt.legend()
plt.show()

# Histogram
plt.figure(figsize=(8, 4))
plt.hist(data, bins=5, rwidth=0.8, alpha=0.7, color='blue')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram Example')
plt.show()
45

# Scatter Plot
plt.figure(figsize=(8, 4))
plt.scatter(x, y, label='Scatter Plot', color='red', marker='o', s=100)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot Example')
plt.legend()
plt.show()

# Area Plot
plt.figure(figsize=(8, 4))
plt.stackplot(x, y, labels=['Area Plot'], alpha=0.7)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Area Plot Example')
plt.legend()
plt.show()

# Pie Plot
labels = ['A', 'B', 'C', 'D', 'E']
plt.figure(figsize=(8, 8))
plt.pie(data, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
plt.title('Pie Plot Example')
plt.show()

OUTPUT
46
47

46. Write a python program to demonstrate Natural Language Processing


CODE
pip install nltk
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.stem import PorterStemmer
from nltk.probability import FreqDist
import string
48

# Download NLTK resources (if not already downloaded)


nltk.download('punkt')
nltk.download('stopwords')

# Sample text for demonstration


sample_text = """
Natural language processing (NLP) is a subfield of artificial intelligence (AI) that focuses on the
interaction
between computers and human language. NLP techniques enable machines to understand,
interpret, and generate
human language, making it possible to perform various tasks such as sentiment analysis,
language translation,
text summarization, and more. NLP has applications in many areas, including chatbots, virtual
assistants,
language modeling, and information retrieval.
"""

# Tokenization: Break the text into sentences and words


sentences = sent_tokenize(sample_text)
words = word_tokenize(sample_text.lower())

# Remove punctuation and stopwords


stop_words = set(stopwords.words('english'))
filtered_words = [word for word in words if word.isalnum() and word not in stop_words]

# Stemming: Reduce words to their base or root form


stemmer = PorterStemmer()
49

stemmed_words = [stemmer.stem(word) for word in filtered_words]

# Frequency Distribution: Count the occurrences of each word


fdist = FreqDist(stemmed_words)

# Print the original text, tokenized words, and frequency distribution


print("Original Text:\n", sample_text)
print("\nTokenized Words:\n", words)
print("\nFiltered Words (without stopwords and punctuation):\n", filtered_words)
print("\nStemmed Words:\n", stemmed_words)
print("\nFrequency Distribution of Words:\n", fdist.most_common(5))

OUTPUT
50

47. Write a python program to demonstrate image processing


CODE:-
import cv2

# Read the image.


image = cv2.imread("C:\\Users\\tgeor\\OneDrive\\Pictures\\digital_camera_photo.jpg")

# Convert the image to grayscale.


gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply a Gaussian blur to the image.


blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)

# Threshold the image.


thresholded_image = cv2.threshold(blurred_image, 127, 255, cv2.THRESH_BINARY)[1]

# Show the original image.


cv2.imshow("Original Image", image)

# Show the grayscale image.


cv2.imshow("Grayscale Image", gray_image)

# Show the blurred image.


cv2.imshow("Blurred Image", blurred_image)

# Show the thresholded image.


cv2.imshow("Thresholded Image", thresholded_image)
51

# Wait for the user to press a key.


cv2.waitKey(0)

# Close all the windows.


cv2.destroyAllWindows()
OUTPUT

48 Write a python program for K Nearest Neighbors Algorithm for Classification


CODE
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
52

# Load and preprocess the data

data = pd.read_csv('C:\\Users\\tgeor\\Downloads\\customer_data.csv')

data.head()

print(data.columns)

OUPUT

X = data.loc[:,['time_spent', 'products_viewed']] # Features

print(X)

OUTPUT

y = data.loc[:,['purchase_made']] # Labels

print(y)

OUTPUT
53

# Split the data into training and testing sets; 20% data is used to test and 8

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random

print(X_train)

print(X_test)

print(y_train)

print(y_test)

# Create and train the KNN model

knn = KNeighborsClassifier(n_neighbors=5) #represents the number of closest ne

knn.fit(X_train, y_train)

# Make predictions on the testing set

y_pred = knn.predict(X_test)

print(y_pred)

# Evaluate the model's accuracy

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)
54

OUPUT

You might also like