Python Programming for Data Science 1
PROGRAM:1
AIM
Write a python program to input the details of a Student (such as Student Id, Name, Age, Email,
Phone Number, Subject, department) and display the details of a student.
PROGRAM
# Input the details of a Student
Studentid=int(input("Enter student id:"))
StudentName=input("Enter Student Name:")
StudentAge=int(input("Enter Student Age:"))
StudentEmail=input("Enter Student Email:")
StudentPhone=int(input("Enter Student PhNo:"))
StudentSubject=input("Enter Student Subject Name:")
StudentDepartment=input("Enter Student Department:")
print() # Blank line for better readability
# Display the student details
print("Student Details:")
print("StudentId:"+str(Studentid))
print("Student Name:"+StudentName)
print("Student Age:"+str(StudentAge))
print("Student Email:"+StudentEmail)
print("Student Phone:"+str(StudentPhone))
print("Student Subject:"+StudentSubject)
print("Student Department:"+StudentDepartment)
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 2
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 3
PROGRAM:2
AIM
Write a python program to find the sum and average of elements in a list.
PROGRAM
numbers = [1, 2, 3, 4, 5, 6, 7]
list_sum=sum(numbers)
# Calculate the average of the elements
list_avg=list_sum/len(numbers)
# Display the sum and average
print("Sum:"+str(list_sum))
print("Average:"+str(list_avg))
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 4
PROGRAM:3
AIM
Write a python program to count Even and Odd numbers in a List.
PROGRAM
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
odd_number = []
even_number = []
for i in numbers:
if i % 2 == 0:
even_number.append(i) # Add even number to the even_number list
else:
odd_number.append(i) # Add odd number to the odd_number list
print("Odd Numbers: " + str(odd_number))
print("Even Numbers: " + str(even_number))
odd = 0
even = 0
for i in numbers:
if i % 2 == 0:
even += 1 # Increment the even count
else:
odd += 1 # Increment the odd count
print("Count of Odd Numbers: " + str(odd))
print("Count of Even Numbers: " + str(even))
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 5
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 6
PROGRAM:4
AIM
Write a python program to print all even and odd numbers in a range.
PROGRAM
start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
even_number=[]
odd_number=[]
for i in range(start,end+1):
if(i%2==0):
even_number.append(i)
else:
odd_number.append(i)
print("Even Numbers:"+str(even_number))
print("odd Numbers:"+str(odd_number))
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 7
PROGRAM:5
AIM
Write a python program to print duplicates from a list of integers.
PROGRAM
number=[1,1,2,2,3,4,5,6,6,7,8,9,4]
seen=set()
duplicate=set()
for i in number:
if i in seen:
duplicate.add(i)
else:
seen.add(i)
print("Duplicates:"+str(duplicate))
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 8
PROGRAM:6
AIM
Write a python program to count positive and negative numbers in a list.
PROGRAM
numbers=[1,-2,-4,-6,4,5,8,9]
possitive=0
negative=0
for i in numbers:
if(i>0):
possitive+=1
else:
negative+=1
print("Possitive number:"+str(possitive))
print("negative number:"+str(negative))
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 9
PROGRAM:7
AIM
Write a python program to print negative numbers in a list.
PROGRAM
numbers=[1,2,3,4,-1,-3,-8,-9,7]
negative_numbers=[]
for i in numbers:
if(i<0):
negative_numbers.append(i)
print("negative_numbers:"+str(negative_numbers))
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 10
PROGRAM:8
AIM
Write a python program to print negative and positive numbers in a list.
PROGRAM
numbers=[1,2,3,4,-1,-3,-8,-9,7]
negative_numbers=[]
possitive_numbers=[]
for i in numbers:
if(i<0):
negative_numbers.append(i)
else:
possitive_numbers.append(i)
print("negative_numbers:"+str(negative_numbers))
print("possitive_numbers:"+str(possitive_numbers))
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 11
PROGRAM:9
AIM
Write a python program to Display the multiplication Table.
PROGRAM
number=int(input("Enter the multiplcation table you want:"))
for i in range(1,11):
print(f"{number} * {i} = {number * i}")
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 12
PROGRAM:10
AIM
Write a python program to Make a Simple Calculator.
PROGRAM
def calculator():
while True:
print("\nSelect Operator")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exit")
try:
choice = int(input("Enter your choice (1-5): "))
if choice == 5:
print("Exiting the calculator. Goodbye!")
break
if choice in [1, 2, 3, 4]:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == 1:
result = num1 + num2
elif choice == 2:
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 13
result = num1 - num2
elif choice == 3:
result = num1 * num2
elif choice == 4:
if num2 != 0:
result = num1 / num2
else:
result = "Error! Division by zero."
print("Result: " + str(result))
else:
print("Invalid Input. Please enter a number between 1 and 5.")
except ValueError:
print("Invalid input. Please enter a valid number.")
calculator()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 14
PROGRAM:11
AIM
Write a python program to Find Factorial of Number Using Recursion.
PROGRAM
def factorial(n):
if n == 1 or n == 0:
return 1
else:
return n*factorial(n-1)
num = int(input("Enter a number"))
if num<0:
print("Factorial is not defined for negative numbers")
else:
print(f"The factorial of {num} is {factorial(num)}")
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 15
PROGRAM:12
AIM
Write a python program to Create Pyramid Patterns.
PROGRAM
def star_piramid(n):
for i in range(n):
print(' ' * (n-i-1),end='')
print('* '*(i+1))
rows=int(input("Enter the number of rows for the star pyramid:"))
star_piramid(rows)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 16
PROGRAM:13
AIM
Write a python program to :
a) Create a dictionary
b) Merge two Dictionary
c) Update Dictionary
d) Add an element to the dictionary
e) Remove Elements from Dictionary
f) Get value and key of a particular item
PROGRAM
#Create a Dictionary
dict1={1:'Adwaith',2:'anjana',3:'arjun'}
dict2={4:'eldho',5:'shibin'}
print(dict1)
print(dict2)
# Merge Two Dictionary
dict1.update(dict2)
print("After Merging :")
print(dict1)
# Update Dictionary
print("Updating elements in a dictionary")
dict1 = {}
dict1.update({1: 'Adwaith Rajeev', 2: 'Anjana Elizebath', 3: 'Arjun k Shine', 4: 'Eldho Elias',
5: 'Shibin Abraham'})
print(dict1)
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 17
#Adding an Element to Dictionary
print("Adding element to a dictionary")
dict1[6]='Amritha'
print(dict1)
#Removing an Element from the Dictionary
print("removing element from a dictionary")
dict1.pop(6)
print(dict1)
#Getting values and Keys of the items in the Dictionary
print("Printing elements with their keys & values")
for i in dict1:
print(i, dict1[i])
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 18
PROGRAM:14
AIM
Write a python program to Check if:
a) a Key is Already Present in a Dictionary
b) a value present in a dictionary
c)an item present in a dictionary
PROGRAM
def check_dictionary():
# Take dictionary input from the user
n = int(input("Enter the number of items in the dictionary: "))
dictionary = {}
for _ in range(n):
key = input("Enter key: ")
value = input("Enter value: ")
dictionary[key] = value
# a) Check if a key is present
key = input("\nEnter the key to check: ")
if key in dictionary:
print(f"Key '{key}' is present in the dictionary.")
else:
print(f"Key '{key}' is not present in the dictionary.")
# b) Check if a value is present
value = input("\nEnter the value to check: ")
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 19
if value in dictionary.values():
print(f"Value '{value}' is present in the dictionary.")
else:
print(f"Value '{value}' is not present in the dictionary.")
# c) Check if an item (key-value pair) is present
item_key = input("\nEnter the key of the item to check: ")
item_value = input("Enter the value of the item to check: ")
if dictionary.get(item_key) == item_value:
print(f"Item ('{item_key}', '{item_value}') is present in the dictionary.")
else:
print(f"Item ('{item_key}', '{item_value}') is not present in the dictionary.")
# Call the function
check_dictionary()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 20
PROGRAM:15
AIM
Write a Python Program to Display Calendar
PROGRAM
import calendar
year=int(input("enter the year"))
month=int(input("enter the month"))
print(calendar.month(year,month))
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 21
PROGRAM:16
AIM
Write a Python program to add two matrices
PROGRAM
x=[]
y=[]
n=int(input("Enter the size of the matrix:"))
print("Enter the element for matrix x:")
for i in range(n):
row=[]
for j in range(n):
element=int(input(f"Enter elements:"))
row.append(element)
x.append(row)
print("Enter the element for matrix y:")
for i in range(n):
row=[]
for j in range(n):
element=int(input(f"Enter elements:"))
row.append(element)
y.append(row)
result=[]
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 22
for i in range(n):
row = []
for j in range(n):
row.append(x[i][j] + y[i][j])
result.append(row)
print("The result of matrix addition is:")
for r in result:
print(r)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 23
PROGRAM:17
AIM
Write a Python program to multiply two matrices
PROGRAM
matrix1=[]
matrix2=[]
n=int(input("Enter the size of the matrix:"))
print("Enter the element for matrix x:")
for i in range(n):
row=[]
for j in range(n):
element=int(input(f"Enter elements:"))
row.append(element)
matrix1.append(row)
print("Enter the element for matrix y:")
for i in range(n):
row=[]
for j in range(n):
element=int(input(f"Enter elements:"))
row.append(element)
matrix2.append(row)
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 24
result=[]
# Multiply the matrices
for i in range(len(matrix1)): # Iterate over rows of matrix1
result.append([]) # Create a new row in the result matrix
for j in range(len(matrix2[0])): # Iterate over columns of matrix2
sum = 0
for k in range(len(matrix2)): # Iterate over rows of matrix2
sum += matrix1[i][k] * matrix2[k][j]
result[i].append(sum) # Append the sum to the current row of the result
# Print the result matrix
print("The product of the matrices is:")
for row in result:
print(row)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 25
PROGRAM:18
AIM
Write a python program to create a class by name Students, and initialise attributes like student id,
name, age, phone number, department, name of college, location while creating an object.
PROGRAM
class Student:
def __init__(self, id, name, age, phoneNo, department, collegeName, location):
self.id = id
self.name = name
self.age = age
self.phoneNo = phoneNo
self.department = department
self.collegeName = collegeName
self.location = location
def show_details(self):
print("Student ID: ", self.id)
print("Name: ", self.name)
print("Age: ", self.age)
print("Phone Number: ", self.phoneNo)
print("Department: ", self.department)
print("College Name: ", self.collegeName)
print("Location: ", self.location)
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 26
# Function to create a student object from user input
def create_student():
print("\nEnter Student Details")
id = int(input("Enter ID: "))
name = input("Enter Name: ")
age = int(input("Enter Age: "))
phoneNo = int(input("Enter Phone Number: "))
department = input("Enter Department: ")
collegeName = input("Enter College Name: ")
location = input("Enter Location: ")
return Student(id, name, age, phoneNo, department, collegeName, location)
# Create student objects
students = []
n = int(input("Enter the number of students: "))
for _ in range(n):
students.append(create_student())
# Display details of all students
print("\nDisplaying Student Details:")
for student in students:
print()
student.show_details()
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 27
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 28
PROGRAM:19
AIM
Write a python program to create a class by name Employee and initialise attributes like
Company name, Location, Employee name, Gender, Profession while creating an object.
PROGRAM
class Employee:
def __init__(self, companyName, location, Ename, gender, profession):
self.companyName = companyName
self.location = location
self.Ename = Ename
self.gender = gender
self.profession = profession
def display_details(self):
print("Company Name:", self.companyName)
print("Location:", self.location)
print("Employee Name:", self.Ename)
print("Gender:", self.gender)
print("Profession:", self.profession)
# Function to create an employee object from user input
def create_employee():
print("\nEnter Employee Details")
companyName = input("Enter Company Name: ")
location = input("Enter Location: ")
Ename = input("Enter Employee Name: ")
gender = input("Enter Gender: ")
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 29
profession = input("Enter Profession: ")
return Employee(companyName, location, Ename, gender, profession)
# Create employee objects
employees = []
n = int(input("Enter the number of employees: "))
for _ in range(n):
employees.append(create_employee())
# Display details of all employees
print("\nDisplaying Employee Details:")
for employee in employees:
print()
employee.display_details()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 30
PROGRAM:20
AIM
Write a python program to perform file handling:
a) Open a file
b) Write to file
c) Append to file
d) Close a file
e) Create a new file
f) Delete a file
PROGRAM
# Opening a new file (or creating it if it doesn't exist)
f = open("abc.txt", "w")
f.close()
# Writing to a file
f = open("abc.txt", "w")
f.write("Learn Python Programming\n") # Writing content to the file
f.write("Learn Java Programming") # This overwrites previous content if any
f.close()
# Appending content to an existing file
f = open("abc.txt", "a")
f.write("Learn Python Programming \n") # Appending new content
f.close()
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 31
# Reading a file
f = open("abc.txt", "r")
read = f.read()
print(read)
f.close()
# Reading only parts of the file
f = open("abc.txt", "r")
print(f.read(5)) # Reading the first 5 characters
f.close()
# Reading one line of the file
f = open("abc.txt", "r")
print(f.readline()) # Reads only the first line
f.close()
# Reading two lines of the file
f = open("abc.txt", "r")
print(f.readline()) # Reads the first line
print(f.readline()) # Reads the second line
f.close()
# Loop through the file line by line
f = open("abc.txt", "r")
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 32
for line in f:
print(line, end='') # Prints each line in the file without extra newlines
f.close()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 33
PROGRAM:21
AIM
Write a python program to Implement Database connectivity using SQLite
a) Creating Database
b) Creating a cursor object
c) Creating a table
d) Inserting records into a table
e) Fetching records from table
f) Updating records
g) Deleting records
h) Deleting table
PROGRAM
import sqlite3
# Step 1: Connect to the database (or create it if it doesn't exist)
conn = sqlite3.connect("sample_database.db")
print("Database created and connected successfully.")
# Step 2: Create a cursor object
cursor = conn.cursor()
# Step 3: Create a table
cursor.execute('''CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 34
grade TEXT)''')
print("Table 'students' created successfully.")
# Step 4: Insert records into the table
cursor.execute("INSERT INTO students (name, age, grade) VALUES ('Alice', 21, 'A')")
cursor.execute("INSERT INTO students (name, age, grade) VALUES ('Bob', 22, 'B')")
cursor.execute("INSERT INTO students (name, age, grade) VALUES ('Charlie', 23, 'C')")
conn.commit() # Commit changes to the database
print("Records inserted successfully.")
# Step 5: Fetch records from the table
cursor.execute("SELECT * FROM students")
rows = cursor.fetchall()
print("Fetching records from 'students' table:")
for row in rows:
print(row)
# Step 6: Update a record
cursor.execute("UPDATE students SET grade = 'A+' WHERE name = 'Bob'")
conn.commit()
print("Record updated successfully.")
# Step 7: Delete a record
cursor.execute("DELETE FROM students WHERE name = 'Charlie'")
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 35
conn.commit()
print("Record deleted successfully.")
# Step 8: Delete the table
cursor.execute("DROP TABLE IF EXISTS students")
conn.commit()
print("Table 'students' deleted successfully.")
# Close the database connection
conn.close()
print("Database connection closed.")
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 36
PROGRAM:22
AIM
Write a python program to perform Python Module.
PROGRAM
employee_module.py
class Employee:
def __init__(self, companyName, location, Ename, gender, profession):
self.companyName = companyName
self.location = location
self.Ename = Ename
self.gender = gender
self.profession = profession
def display_details(self):
print("Company Name:", self.companyName)
print("Location:", self.location)
print("Employee Name:", self.Ename)
print("Gender:", self.gender)
print("Profession:", self.profession)
def create_employee():
print("\nEnter Employee Details")
companyName = input("Enter Company Name: ")
location = input("Enter Location: ")
Ename = input("Enter Employee Name: ")
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 37
gender = input("Enter Gender: ")
profession = input("Enter Profession: ")
return Employee(companyName, location, Ename, gender, profession)
main.py
from employee_module import Employee, create_employee
def main():
# Create employee objects
employees = []
n = int(input("Enter the number of employees: "))
for _ in range(n):
employees.append(create_employee())
# Display details of all employees
print("\nDisplaying Employee Details:")
for employee in employees:
print()
employee.display_details()
if __name__ == "__main__":
main()
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 38
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 39
PROGRAM:23
AIM
Write a Python program to handle a ZeroDivisionError exception when dividing a number by
zero.
PROGRAM
def divide_numbers(x, y):
try:
result = x / y
print("Result:", result)
except ZeroDivisionError:
print("The division by zero operation is not allowed.")
try:
x = float(input("Enter the numerator: "))
y = float(input("Enter the denominator: "))
divide_numbers(x, y)
except ValueError:
print("Please enter valid numbers.")
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 40
PROGRAM:24
AIM
Write a Python program that prompts the user to input an integer and raises a ValueError
exception if the input is not a valid integer.
PROGRAM
def get_integer_input():
try:
user_input = input("Please enter an integer: ")
num = int(user_input)
print(f"You entered a valid integer: {num}")
except ValueError:
print("Error: Invalid input! Please enter a valid integer.")
get_integer_input()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 41
PROGRAM:25
AIM
Write a Python program that prompts the user to input two numbers and raises a TypeError
exception if the inputs are not numerical.
PROGRAM
def get_two_numbers():
try:
num1 = input("Please enter the first number: ")
num2 = input("Please enter the second number: ")
num1 = float(num1)
num2 = float(num2)
print(f"You entered the numbers: {num1} and {num2}")
except ValueError:
raise TypeError("Error: Both inputs must be numerical values.")
try:
get_two_numbers()
except TypeError as e:
print(e)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 42
PROGRAM:26
AIM
Write a Python program that executes division and handles an ArithmeticError exception if there
is an arithmetic error..
PROGRAM
def divide_numbers(x, y):
try:
# Perform division
result = x / y
print("Result:", result)
except ArithmeticError:
print("An arithmetic error occurred. Please check your inputs.")
try:
num1 = float(input("Enter the numerator: "))
num2 = float(input("Enter the denominator: "))
divide_numbers(num1, num2)
except ValueError:
print("Please enter valid numbers.")
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 43
PROGRAM:27
AIM
Write a Python program to perform User defined exception for handling invalid age
PROGRAM
class InvalidAgeError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
def check_age(age):
if age < 0 or age > 150:
raise InvalidAgeError("Age must be between 0 and 150.")
else:
print(f"Your age is: {age}")
try:
age = int(input("Enter your age: ")
check_age(age)
except InvalidAgeError as e:
print(f"Error: {e}")
except ValueError:
print("Please enter a valid integer for age.")
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 44
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 45
PROGRAM:28
AIM
Create a series using panda packages.
a) Create a simple Pandas Series from a list
b) To access items in a series
c) Create Labels
d) To access an item by referring to the label
e) Create a simple Pandas Series from a dictionary
f) Create a DataFrame from two Series
PROGRAM
import pandas as pd
# Checking Pandas Version
print(pd.__version__)
OUTPUT
a) Create a simple Pandas Series from a list
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 46
b) To access items in a series
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
print(myvar[0])
OUTPUT
c)Create Labels
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a, index = ["x", "y", "z"])
print(myvar)
OUTPUT
d)To access an item by referring to the label
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a, index = ["x", "y", "z"])
print(myvar["y"])
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 47
OUTPUT
e)Create a simple Pandas Series from a dictionary
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = pd.Series(calories)
print(myvar)
OUTPUT
f)Create a DataFrame from two Series
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
myvar = pd.DataFrame(data)
print(myvar)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 48
PROGRAM:29
AIM
Create a DataFrame using Pandas packages.
a) Create a simple Pandas DataFrame from a list.
b) Load data into a DataFrame object.
c) Use a list of indexes to assign custom row labels
d) Add a list of names to give each row a specific name.
e) To return specific row data from the DataFrame.
PROGRAM
a)Create a simple Pandas DataFrame from a list.
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
myvar = pd.DataFrame(data)
print(myvar)
OUTPUT
b)Load data into a DataFrame object.
import pandas as pd
data = {
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 49
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
df = pd.DataFrame(data)
print(df.loc[0])
OUTPUT
c)Use a list of indexes to assign custom row labels
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
df = pd.DataFrame(data)
print(df.loc[[0, 1]])
OUTPUT
d)Add a list of names to give each row a specific name
import pandas as pd
data = {
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 50
"calories": [420, 380, 390],
"duration": [50, 40, 45]
df = pd.DataFrame(data, index = ["day1", "day2", "day3"])
print(df)
OUTPUT
e)To return specific row data from the DataFrame.
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
df = pd.DataFrame(data, index = ["day1", "day2", "day3"])
print(df.loc["day2"])
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 51
PROGRAM:30
AIM
Create a data frame by importing CSV files using panda packages.
a) Importing CSV file
b) Analysing data
c) Cleaning data
d) Data Correlations
e) Plotting
PROGRAM
a)Importing CSV file
import pandas as pd
df = pd.read_csv('C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv')
print(df)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 52
b)Analysing data
import pandas as pd
df = pd.read_csv('C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv')
print(df.head(10))
OUTPUT
1. Print the last 5 rows of the DataFrame
import pandas as pd
df =
pd.read_csv('C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv')
print(df.tail())
OUTPUT
2. Print information about the data
import pandas as pd
df =
pd.read_csv('C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv')
print(df.info())
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 53
OUTPUT
c)Cleaning data
1. Remove Rows
One way to deal with empty cells is to remove rows that contain empty cells.
import pandas as pd
pd.read_csv('C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv')
new_df = df.dropna()
print(new_df.to_string())
OUTPUT
2. Replace Using Mean, Median, or Mode
import pandas as pd
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 54
df = pd.read_csv('C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv')
x = df["income"].mean()
df["income"].fillna(x, inplace=True)
print(df.to_string())
3. Removing Duplicates
import pandas as pd
df = pd.read_csv('C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv')
x = df["income"].mean()
df["income"].fillna(x, inplace = True)
print(df.duplicated())
OUTPUT
d)Data Correlations
import pandas as pd
df = pd.read_csv('C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv’)
df.corr()
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 55
OUTPUT
e) Plotting
import pandas as pd
import matplotlib.pyplot as plt
df =
pd.read_csv(''C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv')
df.plot()
plt.show()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 56
Scatter Plot
import pandas as pd
import matplotlib.pyplot as plt
df =
pd.read_csv(‘C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv’')
df.plot(kind = 'scatter', x = 'income', y = 'happiness')
plt.show()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 57
Histogram
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('
C:/Users/Reshbalal/Documents/STAS/MCA/dataset/income.data.csv’'')
df["income"].plot(kind = 'hist')
plt.show()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 58
PROGRAM:31
AIM
Write a Python program for Numeric Analysis using Numpy
PROGRAM
Data type of an Array :
import numpy as np
a = np.array(['A','B','C','D'])
f = np.array([1.5,2.5,3.5,4.5])
i = np.array([1,2,3,4])
print("The Type of Array : ",a.dtype)
print("The Type of Array : ",f.dtype)
print("The Type of Array : ",i.dtype)
OUTPUT
Dimensions ,Multiplication and Size of the Array :
import numpy as np
a1 = np.array([1,2,3,4])
print("This is a one dimension array",a1)
print("dimension of this array",a1.ndim)
print("Multiply two in one dimension array",a1*2)
print ("Type of array ",type(a1))
print ("size of array ",a1.size)
a2=np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("This is a two Dimension array",a2)
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 59
print("Dimension of this array",a2.ndim)
print("Multiple two in two dimension array ",a2*2)
print ("Type of array ",type(a2))
print ("size of array ",a2.size)
a3=np.array([[[1,2,3],[4,5,6],[7,8,9]],
[[2,3,4,],[5,6,7],[8,9,0]],
[[1,3,5],[2,4,6],[1,4,8]]])
print('This is a Three Dimension Array',a3)
print("Dimension of this Array : ",a3.ndim)
print("Multiplying 2 in Three Dimension Array : ",a3 * 2)
print("Type of Array : ",type(a3))
print("Size of Array : ",a3.size)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 60
Shape of an Array :
import numpy as np
a1 = np.array([1,2,3,4])
a2=np.array([[1,2,3,4,5],[6,7,8,9,10]])
a3=np.array([[[1,2,3],[4,5,6],[7,8,9]],
[[2,3,4,],[5,6,7],[8,9,0]],
[[1,3,5],[2,4,6],[1,4,8]]])
print("The Shape of the Array a1 : ",a1.shape)
print("The Shape of the Array a2 : ",a2.shape)
print("The Shape of the Array a3 : ",a3.shape)
OUTPUT
Reshaping :
import numpy as np
a1 = np.array([1,2,3,4])
a2=np.array([[1,2,3,4,5],[6,7,8,9,10]])
a3=np.array([[[1,2,3],[4,5,6],[7,8,9]],
[[2,3,4,],[5,6,7],[8,9,0]],
[[1,3,5],[2,4,6],[1,4,8]]])
na1 = a1.reshape(2,2)
na2 = a2.reshape(10,)
na3 = a3.reshape(3,9)
print("Converting an 1-D array into 2-D Array : ")
print(na1)
print("Converting an 2-D Array into 1-D Array : ")
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 61
print(na2)
print("Converting an 3-D Array into 2-D Array : ")
print(na3)
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 62
PROGRAM:32
AIM
Develop a program based on simple Linear regression.
PROGRAM
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset=pd.read_csv('C:/Users/HP/Desktop/salarydata.csv')
print(dataset.head())
OUTPUT
visualizing the results
1. plot for the TRAIN
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
dataset = pd.read_csv('C:/Users/HP/Desktop/salarydata.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=0)
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 63
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
comparison = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
print(comparison)
plt.scatter(X_train, y_train, color='red')
plt.plot(X_train, regressor.predict(X_train), color='blue')
plt.title("Salary vs Experience (Training set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salaries")
plt.show()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 64
2. Plot for the TEST
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# Step 1: Load the dataset
dataset = pd.read_csv('C:/Users/HP/Desktop/salarydata.csv')
# Data preprocessing
X = dataset.iloc[:, :-1].values # Independent variable (Years of Experience)
y = dataset.iloc[:, 1].values # Dependent variable (Salary)
# Step 2: Splitting the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=0)
# Step 3: Fitting the Linear Regression model to the training set
regressor = LinearRegression()
regressor.fit(X_train, y_train) # Fits the model (finds the line of best fit)
# Step 4: Predicting the test set results
y_pred = regressor.predict(X_test)
# Step 5: Print predicted vs actual values
comparison = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
print(comparison)
# Step 7: Plotting the test data and model
plt.scatter(X_test, y_test, color='green') # Plotting the actual test data points in green
plt.plot(X_train, regressor.predict(X_train), color='blue') # Using the regression line
from the training set
plt.title("Salary vs Experience (Test set)") # Title of the graph
plt.xlabel("Years of Experience") # Label for the x-axis
plt.ylabel("Salaries") # Label for the y-axis
plt.show() # Display the plot
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 65
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 66
PROGRAM:33
AIM
Implementation of KNN classification using Scikit-learn
PROGRAM
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import numpy as np
import matplotlib.pyplot as plt
irisData = load_iris()
# Create feature and target arrays
X = irisData.data
y = irisData.target
# Split into training and test set
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = 0.2, random_state=42)
neighbors = np.arange(1, 9)
train_accuracy = np.empty(len(neighbors))
test_accuracy = np.empty(len(neighbors))
# Loop over K values
for i, k in enumerate(neighbors):
knn = KNeighborsClassifier(n_neighbors=k)
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 67
knn.fit(X_train, y_train)
# Compute training and test data accuracy
train_accuracy[i] = knn.score(X_train, y_train)
test_accuracy[i] = knn.score(X_test, y_test)
# Generate plot
plt.plot(neighbors, test_accuracy, label = 'Testing dataset Accuracy')
plt.plot(neighbors, train_accuracy, label = 'Training dataset Accuracy')
plt.legend()
plt.xlabel('n_neighbors')
plt.ylabel('Accuracy')
plt.show()
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 68
PROGRAM:34
AIM
Implementation of K-Means Clustering algorithm using scikit-learn
PROGRAM
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# 1. Load the Iris dataset
iris = load_iris()
X = iris.data # Features (sepal length, sepal width, petal length, petal width)
# 2. Apply KMeans Clustering
kmeans = KMeans(n_clusters=3, random_state=42) # Create a KMeans model with 3 clusters
kmeans.fit(X) # Fit the KMeans model to the data
# 3. Get the cluster centers (centroids)
centroids = kmeans.cluster_centers_
# 4. Predict the clusters for each data point
y_kmeans = kmeans.predict(X) # Assign each point to a cluster
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 69
# 5. Visualize the clusters (only using two features for simplicity)
# For simplicity, let's use the first two features: sepal length and sepal width
X_2D = X[:, :2] # Only take the first two features (sepal length and sepal width)
# Plotting the data points and centroids
plt.figure(figsize=(8, 6))
# Scatter plot of the data points, color-coded by cluster
plt.scatter(X_2D[:, 0], X_2D[:, 1], c=y_kmeans, cmap='viridis', s=50)
# Mark the cluster centroids
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=200, marker='x', label='Centroids')
plt.title('K-Means Clustering (Iris Dataset)')
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.legend()
plt.show()
# 6. Output the cluster assignments and centroids
print(f"Cluster Centers (Centroids):\n{centroids}")
print(f"Cluster assignments for each data point:\n{y_kmeans}")
MCA 2023-2025 STAS PULLARIKUNNU
Python Programming for Data Science 70
OUTPUT
MCA 2023-2025 STAS PULLARIKUNNU