0% found this document useful (0 votes)
11 views49 pages

Updated Python With Data Science Lab Manual

The document lists various Python programming experiments, including tasks such as computing GCD, finding max/min of a list, string operations, and implementing OOP concepts. Each experiment includes an aim, algorithm, program code, and output examples. The document serves as a comprehensive guide for practicing Python programming skills.

Uploaded by

sec22ad087
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views49 pages

Updated Python With Data Science Lab Manual

The document lists various Python programming experiments, including tasks such as computing GCD, finding max/min of a list, string operations, and implementing OOP concepts. Each experiment includes an aim, algorithm, program code, and output examples. The document serves as a comprehensive guide for practicing Python programming skills.

Uploaded by

sec22ad087
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

List of Experiments

1.​ Write a program to compute the GCD of Two Numbers


2.​ Find the maximum and minimum of a list of Numbers
3.​ Write a Python program to perform the following operations on the given string
a) Count the number of words in the given String
b) Count the number of Vowels in the given String
c) Capitalize the first character of each word
4.​ Write a python program using dictionary to store student marks and find highest
mark
5.​ Find the most frequent words in a text from a file
6.​ Write a program to implement Exception Handling for License Process
7.​ Write a program to implement Classes and Objects for a Student class
8.​ Create packages and import modules from packages
9.​ Write a program to implement OOP concept - Data hiding
10.​Write a program to implement OOP concept - Data abstraction
11.​Write a program to handle numerical operations using math and
random number functions
12.​Create user-defined functions with different types of function
arguments.
13.​ Create NumPy arrays from Python Data Structures, Intrinsic NumPy
objects and Random Functions.
14.​ Manipulation of NumPy arrays- Indexing, Slicing, Reshaping, Joining
and Splitting.
15.​Create Pandas Series and DataFrame from various inputs.
16.​Import any CSV file to Pandas DataFrame and perform the following:
a. Visualize the first and last 10 records
b. Get the shape, index and column details
c. Select/Delete the records (rows)/columns based on conditions.
d. Perform ranking and sorting operations.
e. Do required statistical operations on the given columns.
f. Find the count and uniqueness of the given categorical values.
g. Rename single/multiple columns
17.​ Import any CSV file to Pandas Data Frame and perform the following:
a. Handle missing data by detecting and dropping/ filling missing
values.
b. Transform data using apply () and map() method
c. Detect and filter outliers.
d. Perform Vectorized String operations on Pandas Series.
e. Visualize data using Line Plots, Bar Plots, Histograms, Density Plots
and Scatter Plots.
Ex. No. 1​ ​ ​ FIND GCD OF TWO NUMBERS

Date:​​ ​ ​ ​ ​ ​ ​ ​

AIM: To write a python program for find GCD of two numbers


Algorithm
Step 1: Read two numbers as d1 and d2
Step 2: Calculate the remainder rem, when dividing d1 by d2
Step 3: Run a loop till rem is greater than 0
a)​ Copy d2 to d1
b)​ Copy d2 to rem
c)​ Calculate the remainder rem, when dividing d1 by d2
Step 4: Print value of GCD as d2

Program

# GCD of two numbers

d1=int(input("Enter a number: "))


d2=int(input("Enter another number: "))
rem=d1%d2
while rem > 0 :
d1=d2
d2=rem
rem=d1%d2
print ("The GCD of given numbers is: ",d2)

Output:
Enter a number: 30
Enter another number: 12
d1= 12 d2= 6
The GCD of given numbers is: 6
Ex. No. 2​ ​ MAXIMUM AND MINIMUM OF A LIST OF NUMBERS

Date:​​ ​ ​ ​ ​ ​ ​ ​

AIM: To find the maximum and minimum of a list of Numbers

Algorithm
Step 1: Get a list of elements
Step 2: Initialize min and max variable
Step 3: Run a loop in the iteration of (i) between 0 and length of the list
a)​ compare ith element with max value find max value
b)​ compare list of element with min value find min value
Step 4 : print the result

Program:
# find min and max value from list
a = [3, 5, 7, 2, 8, 1]
# Initializing variables for max and min
mxv = a[0]
mnv = a[0]
mxp = 0
mnp = 0

# Loop through the list to find max and min


for i in range(len(a)):
if a[i] > mxv:
mxv = a[i]
mxp = i
if a[i] < mnv:
mnv = a[i]
mnp = i
print("Maximum Element is ", mxv , "is located in ", mxp, " Position")
print("Minimum Element is ", mnv , "is located in ", mnp, " Position")
Output:
Maximum Element is 8 is located in 4 Position
Minimum Element is 1 is located in 5 Position
Ex. No. 3 STRING OPERATIONS

Date :

Aim:

To Write a Python program to perform the following operations on the given string

a) Count the number of words in the given String

b) Count the number of Vowels in the given String

c) Capitalize the first character of each word

Algorithm:

Step 1: Read the input as String

Step 2: Separate the string into words using “split” function- A list is generated

Step 3: Print the number of words as length of the list generated

Step 4: Traverse the string character by character and check for vowels. Increment

the COUNT variable for each vowel.

Step 5: Print the value of COUNT variable

Step 6: For every element in the list generated, use title() function to capitalize the

first word

Program:

s=input(“Enter a string:”)

l=[Link]()

print(“Given String :”,s)


print(“Number of words =”,len(l))

COUNT=0

for i in s:

if (i in “aeiouAEIOU”):

COUNT=COUNT+1

print(“Number of Vowels =”,COUNT)

for i in l:

print([Link](),end=” “)

Output:

Enter a string: i love python

Given String : i love python

Number of words = 3

Number of Vowels = 4

I Love Python
Ex. No. 4 DICTIONARY MANIPULATION

Date :

Aim:
​ To write a python program using dictionary to store student marks and find
highest mark
Algorithm:

1.​ Create a dictionary to store student marks


2.​ Assign values to students
3.​ Using a loop find the highest mark, by iterating every key : value pair
4.​ Print the highest mark along with student name

Program :

student_marks = {
"Alice": 85,
"Bob": 92,
"Charlie": 78,
"David": 88,
"Eve": 91
}
# Find the student with the highest mark
for student, marks in student_marks.items():
if marks > highest_mark:
highest_mark = marks
highest_student = student

# Print the highest mark and the student who obtained it


print("The student with the highest mark is ", highest_student , " with "
,highest_mark," marks.")

Output:

The student with the highest mark is Bob with 92 marks.


Ex. No. 5 MOST FREQUENT WORDS IN A FILE

Aim:

To implement a program to find the most frequent words in a text read from a file.

Algorithm:

1.​ Variable maxCount will store the count of most repeated word.
2.​ Open a file in read mode using file pointer.
3.​ Read a line from file. Convert each line into lowercase and remove the
punctuation marks.
4.​ Split the line into words and store it in an array.
5.​ Use two loops to iterate through the array. Outer loop will select a word which
needs to be count. Inner loop will match the selected word with rest of the
array. If match found, increment count by 1.
6.​ If count is greater than maxCount then, store value of count in maxCount and
corresponding word in variable word.
7.​ At the end, maxCount will hold the maximum count and variable word will
hold most repeated word.

Program:

count = 0
word = ""
maxCount = 0
file = open("[Link]", "r")
s=[Link]()
words = [Link]()
for i in range(0,len(words)):
count = 1
for j in range(i+1,len(words)):
if(words[i] == words[j]):
count = count + 1
if(count >maxCount):
maxCount = count
word = words[i]
print("Most repeated word: " + word , maxcount , “ times” )
[Link]()

Output:
Most Repeated Word: Computer 4 times
Ex. No. 6​ ​ ​ EXCEPTION HANDLING

Date :

Aim:

To implement exception handling for license process in python

Algorithm:

Step 1: Read the input sentence.


Step 2: Read the necessary input values
Step 3: Use try and except to perform the validation of inputs
Step 4: Print the result.
Step 5: Stop

Program:

print("License Process")
name = input("Name : ")
bloodgroup = input("Blood Group : ")
address = input("Address : ").strip()
while(True):
try:
age = int(input("Age: "))
break
except:
print("Enter only numbers")

while(True):
try:
aadharNumber = eval(input("Aadhar Number: "))
if len(str(aadharNumber)) ==12:
break
else:
print("Your aadhar number must have 12 digits")
except:
print("Enter only number")
print("[Link] Wheeler")
print("[Link] Wheeler")
vehicle = int(input("Enter your details:"))
if age >= 12 and age <= 60:
print("Successfully submitted the application")
else:
print("Your are not eligible for the license")

Output:

License Process
Name : aaa
Blood Group : a
Address : weqqe
Age: 12
Aadhar Number: s
Enter only number
Aadhar Number: 12
Your aadhar number must have 12 digits
Aadhar Number: 123333333333
[Link] Wheeler
[Link] Wheeler
Enter your details:1
Successfully submitted the application
Ex. No. 7​ ​ ​ CLASSES AND OBJECTS

Date :

Aim:

To implement Classes and objects for Student Class

Algorithm:

Step 1: To create a class, use the keyword class

Step 2: we can use the class named MyClass to create objects

Step 3: Use the init () function to assign values to object properties, or other
operations that are necessary to do when the object is being created.

Step 4: The self parameter is a reference to the current instance of the class, and is
used to access variables that belong to the class.

Program:

Class Student:
Type = 'Student'
def __init (self, id, dpet):
​ [Link] = id
​ [Link] = dept
S1= Student(104, "CSE")
S2 = Student(205, "IT")
print('Student 1 details:')
print('Type is :', [Link])
print('ID: ', [Link])
print('Dept: ', [Link])

print('Student 2 details:')
print('Type is:', [Link])
print('ID: ', [Link])
print('Dept: ', [Link])
print("\nAccessing class variable using class name")
print(Student .Type)

Output:

Student 1 details:
Type is Student
ID: 104
Dept: CSE

Student 2 details:
Type is Student
ID: 205
Dept: IT
Ex. No. 8​ ​ PACKAGES AND MODULES

Date :

Aim : To create user defined packages and use in a program

Algorithm:
1.​ Create my_package folder and create files with .py extension having user
defined modules
2.​ Add the required modules of the packages in main program file
3.​ Use modules from the package by calling them

Program:

[Link]

def greet(name):
return f"Hello, {name}! Welcome to Python Packages."
[Link]

def add(a, b):


return a + b
def multiply(a, b):
return a * b
[Link]

from my_package import module1, module2


print([Link]("Alice"))
print("Addition:", [Link](5, 3))
print("Multiplication:", [Link](4, 6))

Output :

Hello, Alice! Welcome to Python Packages.

Addition: 8

Multiplication: 24
Ex. No. 9 ​ ​ ​ OOPS CONCEPT - DATA HIDING

Date :

Aim : To write a python program to demonstrate data hiding

Algorithm:

1.​ Create a class for Bank account


2.​ Create methods for deposit and printing balance
3.​ Create object for the class
4.​ Call the methods with the object
5.​ Check that printing data member results in an error

Program:

class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private variable

def deposit(self, amount):


self.__balance += amount

def get_balance(self):
return self.__balance # Controlled access to balance

account = BankAccount(1000)

✅ Allowed
[Link](500)

❌ Error: Can't access private variable


print(account.get_balance()) #
# print(account.__balance) #

Output:
1500
Ex. No. 10​ ​ ​ OOPS CONCEPT - DATA ABSTRACTION

Date :

Aim : To write a python program to implement data hiding

Algorithm:

1.​ Create a class which has an abstract method by importing ABC package
2.​ Override the abstract method in inherited classes
3.​ Create object for inherited classes
4.​ Call the abstract module for each object

Program:

from abc import ABC, abstractmethod


class Car(ABC): ​#abstract class
def mileage(self): #abstract method
pass

class Tesla(Car):
def mileage(self):
print("The mileage is 30kmph")

class Suzuki(Car):
def mileage(self):
print("The mileage is 25kmph ")

class Duster(Car):
def mileage(self):
print("The mileage is 24kmph ")

class Renault(Car):
def mileage(self):
print("The mileage is 27kmph ")

t= Tesla ()
[Link]()

r = Renault()
[Link]()

s = Suzuki()
[Link]()

d = Duster()
[Link]()

Output

The mileage is 30kmph


The mileage is 27kmph
The mileage is 25kmph
The mileage is 24kmph
Ex. No. 11​ MATH AND RANDOM NUMBER MODULE FUNCTIONS
Date :

Aim:

To write a Python program that performs numerical operations using the math
module for mathematical functions and the random module to generate random
numbers.

Algorithm:

Step1: Import the necessary modules: math and random.

Step 2: Generate two random numbers within a specified range (e.g., 1 to 100).

Step 3: Apply mathematical functions from the math module:

Square root

Power function

Logarithm (base 10)

Trigonometric functions (sine, cosine, tangent)

Step 4: Display the results of all operations.

Program:

import math

import random

# Generate two random numbers

num1 = [Link](1, 100)

num2 = [Link](1, 100)

print(f"Random Numbers: {num1}, {num2}")


# Square root of the first number

print(f"Square Root of {num1}: {[Link](num1):.2f}")

# Power function (num1 raised to num2)

print(f"{num1} raised to {num2}: {[Link](num1, num2)}")

# Logarithm (base 10) of num1

print(f"Log base 10 of {num1}: {math.log10(num1):.2f}")

# Generate a random angle in degrees and convert it to radians

angle = [Link](0, 90) # Random angle between 0 and 90

radian = [Link](angle)

# Compute trigonometric values

print(f"Sine of {angle} degrees: {[Link](radian):.2f}")

print(f"Cosine of {angle} degrees: {[Link](radian):.2f}")

print(f"Tangent of {angle} degrees: {[Link](radian):.2f}")

Output:

Random Numbers: 25, 42


Addition: 25 + 42 = 67
Subtraction: 25 - 42 = -17
Multiplication: 25 * 42 = 1050
Division: 25 / 42 = 0.60
Square Root of 25: 5.00
25 raised to 42: 7.025812172263438e+58
Log base 10 of 25: 1.40
Sine of 60 degrees: 0.87
Cosine of 60 degrees: 0.50
Tangent of 60 degrees: 1.73
Ex. No. 12​ ​ ​ TYPES OF FUNCTION ARGUMENTS

Date :

Aim:
To create user-defined functions with different types of function Arguments
and call them from program.
Algorithm:
1.​ Define a function which can take any type of argument.
2.​ Print the values for positional arguments.
3.​ Print the values for keyword arguments.
4.​ Print the values for default arguments.
5.​ Print the values for variable length arguments.
6.​ Call the function by using different kinds of arguments.
7.​ Run the program and verify the output.
Program:
def demo_function(a, b, c=10, *args, d=20, **kwargs):
print(f"Positional Arguments: a={a}, b={b}")
print(f"Default Argument: c={c}")
print(f"Variable-Length Positional Arguments (*args): {args}")
print(f"Keyword-Only Argument with Default: d={d}")

# Calling the function in different ways

# 1. Positional Arguments
print("Example 1: Positional Arguments")
demo_function(1, 2)

# 2. Positional + Default Arguments


print("\nExample 2: Positional + Default Argument")
demo_function(1, 2, 30)

# 3. Using Variable-Length Positional Arguments (*args)


print("\nExample 3: Using *args")
demo_function(1, 2, 30, 40, 50, 60)
# 4. Using Keyword Arguments
print("\nExample 4: Using Keyword Arguments")
demo_function(1, 2, c=15, d=25)

Output

Example 1: Positional Arguments


Positional Arguments: a=1, b=2
Default Argument: c=10
Variable-Length Positional Arguments (*args): ()
Keyword-Only Argument with Default: d=20

Example 2: Positional + Default Argument


Positional Arguments: a=1, b=2
Default Argument: c=30
Variable-Length Positional Arguments (*args): ()
Keyword-Only Argument with Default: d=20

Example 3: Using *args


Positional Arguments: a=1, b=2
Default Argument: c=30
Variable-Length Positional Arguments (*args): (40, 50, 60)
Keyword-Only Argument with Default: d=20

Example 4: Using Keyword Arguments


Positional Arguments: a=1, b=2
Default Argument: c=15
Variable-Length Positional Arguments (*args): ()
Keyword-Only Argument with Default: d=25


Ex. No: 13​ CREATING NUMPY ARRAYS FROM PYTHON DATA
STRUCTURES

Date:

Aim:

To write a python program to create NumPy arrays from Python Data


Structures, Intrinsic NumPy objects and Random Functions.

Algorithm:

Step 1: Import Libraries:


Import numpy for using NumPy arrays.
Step 2: Create NumPy Arrays
Step 3: From Python Data Structures​
We use lists and tuples to create NumPy arrays using [Link]().
Step 4: Using Intrinsic NumPy Objects​
NumPy provides methods like [Link](), [Link](), [Link](),
[Link]() to create arrays.
Step 5: Using Random Functions​
NumPy includes a module, [Link], to generate random numbers. Arrays
can be generated using functions like [Link](), [Link]()
Source Code:

import numpy as np
# From a Python list
array_from_list = [Link]([1, 2, 3, 4, 5])
# From a Python tuple
array_from_tuple = [Link]((10, 20, 30))
# From a nested Python list (multi-dimensional array)
array_from_nested_list = [Link]([[1, 2], [3, 4], [5, 6]])
print(array_from_list)
print(array_from_tuple)
print(array_from_nested_list)
# Array of zeros
array_of_zeros = [Link]((3, 3))
# Array of ones
array_of_ones = [Link]((2, 4))
# Array with values within a range
array_arange = [Link](0, 10, 2)
# Array with evenly spaced values
array_linspace = [Link](0, 1, 5)
print(array_of_zeros)
print(array_of_ones)
print(array_arange)
print(array_linspace)
# Random values between 0 and 1
random_array = [Link](3, 3)
# Random integers within a range
random_integers = [Link](0, 10, (2, 4))
# Random values from a normal distribution
random_normal = [Link](3, 3)
print(random_array)
print(random_integers)
print(random_normal)
Output:

[1 2 3 4 5]
[10 20 30]
[[1 2]
[3 4]
[5 6]]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[0 2 4 6 8]
[0. 0.25 0.5 0.75 1. ]
[[0.753432 0.84873521 0.24197781]
[0.17747662 0.7981542 0.00641123]
[0.43488292 0.36263485 0.90986592]]
[[5 5 3 0]
[7 3 6 5]]
[[ 0.19532656 0.50074958 0.91183389]
[-1.00408449 -1.3168787 -0.01482787]
[ 0.99549621 1.31897228 -2.27609934]]
Ex. No: 14​ ​ ​ MANIPULATION OF NUMPY ARRAYS

Date:

Aim

To demonstrate various NumPy array manipulation techniques, including


indexing, slicing, reshaping, and joining arrays using Python.

Algorithm

1.​ Import the NumPy library.


2.​ Create NumPy arrays using [Link]().
3.​ Perform Indexing to access specific elements in the array.
4.​ Perform Slicing to extract specific sections of the array.
5.​ Reshape the array using reshape() to modify dimensions.
6.​ Join arrays using concatenate() to combine multiple arrays.
7.​ Print the results of each operation.

Source Code

import numpy as np
arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Original Array:\n", arr)
print("\nElement at row 1, column 2:", arr[1, 2])
print("\nFirst two rows:\n", arr[:2])
print("\nLast two columns:\n", arr[:, -2:])
reshaped_arr = [Link](1, 9)
print("\nReshaped Array (1x9):\n", reshaped_arr)
arr2 = [Link]([[10, 11, 12]])
joined_arr = [Link]((arr, arr2), axis=0) # Join along rows
print("\nJoined Array:\n", joined_arr)

Output

Original Array:
[[1 2 3]
[4 5 6]
[7 8 9]]

Element at row 1, column 2: 6

First two rows:


[[1 2 3]
[4 5 6]]

Last two columns:


[[2 3]
[5 6]
[8 9]]

Reshaped Array (1x9):


[[1 2 3 4 5 6 7 8 9]]

Joined Array:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Ex. No: 15​ ​ CREATING PANDAS SERIES AND DATAFRAME

Date :

Aim:

To write a python program to create Pandas Series and DataFrame from


various inputs.

●​ Pandas Series: Can be created from lists, dictionaries, etc., and is similar to
a 1D array.
●​ Pandas DataFrame: Can be created from lists, dictionaries, or NumPy
arrays, and is like a 2D table with rows and columns.

Algorithm:

1.​ Import Libraries:


o​ Import pandas as pd and optionally numpy if using NumPy arrays.
2.​ Prepare the Input Data:
o​ Accept data as lists, dictionaries, or arrays.
o​ For MultiIndex, generate a MultiIndex using
[Link].from_tuples().
3.​ Convert the Input to Series or DataFrame:
o​ Use [Link]() for creating a Series.
o​ Use [Link]() for creating a DataFrame.
4.​ Optional: Set Index and Column Names:
o​ For DataFrames, specify column names.
o​ For Series, optionally set an index.
5.​ Output:
o​ Print or return the created Series or DataFrame.

Source Code:
# Creating a DataFrame from a Series
import pandas as pd
s = [Link]([10, 20, 30, 40, 50])
print(s)
s = [Link]([10, 20, 30, 40, 50], name="Numbers")
print(s)
# Using Multiple Series to create a DataFrame
s1 = [Link]([10, 20, 30, 40, 50], name="Numbers")
s2 = [Link](["apple", "orange", "banana", "grape", "watermelon"],
name="Fruits")
df = [Link]([s1, s2], axis=1)
print(df)
# Adding a new column to an existing DataFrame
df = [Link]({"Numbers": [10, 20, 30, 40, 50], "Fruits": ["apple",
"orange", "banana", "grape", "watermelon"]})
new_col = [Link]([5, 4, 3, 2, 1], name="Ranks")
df = [Link]([df, new_col], axis=1)
print(df)
Output:

0 10
1 20
2 30
3 40
4 50

dtype: int64

0 10
1 20
2 30
3 40
4 50

Name: Numbers, dtype: int64

Numbers Fruits
0 10 apple
1 20 orange
2 30 banana
3 40 grape
4 50 watermelon

Numbers Fruits Ranks


0 10 apple 5
1 20 orange 4
2 30 banana 3
3 40 grape 2
4 50 watermelon 1
[Link]​ ​ ​ DATA MANIPULATION WITH PANDAS

Date:​

Aim:

To write a python program to perform data manipulation using pandas.

Algorithm:

Step 1: Start.

Step 2: Import Required Libraries:

Step 3: Load the Data:

Step 4: Inspect the Data:

​ Step 5: Get the shape, index and column details

Step 6: Select/Delete the records (rows)/columns based on conditions.

Step 7: Perform ranking and sorting operations

Step 8: Do required statistical operations on the given columns.

Step 9: Find the count and uniqueness of the given categorical values.

Step 10: Rename single/multiple columns

Step 11: Stop the program.


Source Code:

import pandas as pd
# Load the CSV file into a DataFrame
df = pd.read_csv('sample_data.csv') # Replace with your actual file path

# A. Display the first and last 10 records


print("\nFirst 5 Records:")
print([Link](5))
print("\nLast 5 Records:")
print([Link](5))

Output :

First 5 Records:
ID Name Age City
0 1 Alice 25 New York
1 2 Bob 30 Los Angeles
2 3 Charlie 35 Chicago
3 4 Diana 28 Houston
4 5 Frank 48 Los Angeles

Last 5 Records:
ID Name Age City
5 6 Grace 34 San Diego
6 7 Jack 39 Los Angeles
7 8 Alice 29 Chicago
8 9 Eve 38 Phoenix
9 10 Alice 40 Los Angeles

# [Link] the shape, index, and column details


print(f"\nShape: {[Link]}")
print(f"\nIndex: {[Link]}")
print(f"\nColumns: {[Link]}")

Output :

Shape: (10, 4)
Index: RangeIndex(start=0, stop=10, step=1)
Columns: Index(['ID', 'Name', 'Age', 'City'], dtype='object')
#C. Select rows where Age > 30
selected_rows = df[df['Age'] > 30]
print("\nRecords where Age > 30:")
print(selected_rows)

# [Link] records where Age < 30


df_filtered = df[df['Age'] >= 30]
print("\nDataFrame after deleting records where Age < 30:")
print(df_filtered)

# [Link] specific columns


selected_columns = df[['Name', 'Age']]
print("\nSelected Columns (Name, Age):")
print(selected_columns)

# [Link] the 'City' column


df_dropped = [Link](columns=['City'])
print("\nDataFrame after dropping 'City' column:")
print(df_dropped)

Output :

Records where Age > 30:


ID Name Age City
2 3 Charlie 35 Chicago
4 5 Frank 48 Los Angeles
5 6 Grace 34 San Diego
6 7 Jack 39 Los Angeles
8 9 Eve 38 Phoenix
9 10 Alice 40 Los Angeles

DataFrame after deleting records where Age < 30:


ID Name Age City
1 2 Bob 30 Los Angeles
2 3 Charlie 35 Chicago
4 5 Frank 48 Los Angeles
5 6 Grace 34 San Diego
6 7 Jack 39 Los Angeles
8 9 Eve 38 Phoenix
9 10 Alice 40 Los Angeles

Selected Columns (Name, Age):


Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
3 Diana 28
4 Frank 48
5 Grace 34
6 Jack 39
7 Alice 29
8 Eve 38
9 Alice 40

DataFrame after dropping 'City' column:


ID Name Age
0 1 Alice 25
1 2 Bob 30
2 3 Charlie 35
3 4 Diana 28
4 5 Frank 48
5 6 Grace 34
6 7 Jack 39
7 8 Alice 29
8 9 Eve 38
9 10 Alice 40

#D. Sorting and Ranking


sorted_by_age_asc = df.sort_values(by='Age', ascending=True)
print("\nDataFrame sorted by Age (ascending):")
print(sorted_by_age_asc)

sorted_by_age_desc = df.sort_values(by='Age', ascending=False)


print("\nDataFrame sorted by Age (descending):")
print(sorted_by_age_desc)

df['Age Rank'] = df['Age'].rank(ascending=True)


print("\nDataFrame with Age Rank:")
print(df[['Name', 'Age', 'Age Rank']])
Output :
DataFrame sorted by Age (ascending):
ID Name Age City Age Rank
0 1 Alice 25 New York 1.0
3 4 Diana 28 Houston 2.0
7 8 Alice 29 Chicago 3.0
1 2 Bob 30 Los Angeles 4.0
5 6 Grace 34 San Diego 5.0
2 3 Charlie 35 Chicago 6.0
8 9 Eve 38 Phoenix 7.0
6 7 Jack 39 Los Angeles 8.0
9 10 Alice 40 Los Angeles 9.0
4 5 Frank 48 Los Angeles 10.0

DataFrame sorted by Age (descending):


ID Name Age City Age Rank
4 5 Frank 48 Los Angeles 10.0
9 10 Alice 40 Los Angeles 9.0
6 7 Jack 39 Los Angeles 8.0
8 9 Eve 38 Phoenix 7.0
2 3 Charlie 35 Chicago 6.0
5 6 Grace 34 San Diego 5.0
1 2 Bob 30 Los Angeles 4.0
7 8 Alice 29 Chicago 3.0
3 4 Diana 28 Houston 2.0
0 1 Alice 25 New York 1.0

DataFrame with Age Rank:


Name Age Age Rank
0 Alice 25 1.0
1 Bob 30 4.0
2 Charlie 35 6.0
3 Diana 28 2.0
4 Frank 48 10.0
5 Grace 34 5.0
6 Jack 39 8.0
7 Alice 29 3.0
8 Eve 38 7.0
9 Alice 40 9.0
# [Link] Operations
mean_age = df['Age'].mean()
median_age = df['Age'].median()
std_age = df['Age'].std()
print(f"\nMean Age: {mean_age}")
print(f"\nMedian Age: {median_age}")
print(f"\nStandard Deviation of Age: {std_age}")

summary = [Link]()
print("\nSummary Statistics:")
print(summary)

Output :
Mean Age: 34.6

Median Age: 34.5

Standard Deviation of Age: 6.899275324264136

Summary Statistics:
ID Age Age Rank
count 10.00000 10.000000 10.00000
mean 5.50000 34.600000 5.50000
std 3.02765 6.899275 3.02765
min 1.00000 25.000000 1.00000
25% 3.25000 29.250000 3.25000
50% 5.50000 34.500000 5.50000
75% 7.75000 38.750000 7.75000
max 10.00000 48.000000 10.00000

# [Link] the count and uniqueness of categorical values


city_counts = df['City'].value_counts()
print("\nCount of Each City:")
print(city_counts)

unique_cities = df['City'].unique()
print("\nUnique Cities:")
print(unique_cities)
Output :

Count of Each City:


City
Los Angeles 4
Chicago 2
New York 1
Houston 1
San Diego 1
Phoenix 1
Name: count, dtype: int64

Unique Cities:
['New York' 'Los Angeles' 'Chicago' 'Houston' 'San Diego' 'Phoenix']

# [Link] columns
df_renamed_single = [Link](columns={'Name': 'Full Name'})
print("\nDataFrame after Renaming 'Name' to 'Full Name':")
print(df_renamed_single)

df_renamed_multiple = [Link](columns={'ID': 'Identifier', 'Age': 'Years Old'})


print("\nDataFrame after Renaming Multiple Columns:")
print(df_renamed_multiple)

Output :
DataFrame after Renaming 'Name' to 'Full Name':
ID Full Name Age City Age Rank
0 1 Alice 25 New York 1.0
1 2 Bob 30 Los Angeles 4.0
2 3 Charlie 35 Chicago 6.0
3 4 Diana 28 Houston 2.0
4 5 Frank 48 Los Angeles 10.0
5 6 Grace 34 San Diego 5.0
6 7 Jack 39 Los Angeles 8.0
7 8 Alice 29 Chicago 3.0
8 9 Eve 38 Phoenix 7.0
9 10 Alice 40 Los Angeles 9.0
DataFrame after Renaming Multiple Columns:
Identifier Name Years Old City Age Rank
0 1 Alice 25 New York 1.0
1 2 Bob 30 Los Angeles 4.0
2 3 Charlie 35 Chicago 6.0
3 4 Diana 28 Houston 2.0
4 5 Frank 48 Los Angeles 10.0
5 6 Grace 34 San Diego 5.0
6 7 Jack 39 Los Angeles 8.0
7 8 Alice 29 Chicago 3.0
8 9 Eve 38 Phoenix 7.0
9 10 Alice 40 Los Angeles 9.0
[Link]: 17 ​ DATA CLEANING, PREPARATION AND
VISUALIZATION
DATE:

Aim:
To implement data cleaning, preparation and visualization in python using
pandas.

Algorithm:
Step 1. Read the necessary csv files
Step 2: import in the Python environment with the required libraries installed
(pandas, numpy, matplotlib, seaborn).
Step 3: Include code to handle missing data to detect , fill and remove records
with missing values
Step 4 : Include code to perform data transformation using apply(), map() and
replace methods
Step 5: Include code to Detect and Filter Outliers using Z-transformation
Step 6: Include code to Perform Vectorized String Operations on Pandas Series,
which can handle Null values
Step 7 : Data Visualization is implemented using Line plot, Bar plot, Histogram,
Density plot and Scatter plot with necessary input data.
Step 8 : Check the output for all the operations

Source Code:
# A. Handle Missing Data
import pandas as pd
df=pd.read_csv("[Link]")
print("Original DataFrame:")
print(df)
print([Link]())

# Detect missing values


df_missing = [Link]().sum()
print("\nMissing values in each column:")
print(df_missing)

# Drop rows with missing values


df_droppedna = [Link]() # [Link](inplace=True)to change in the same df
print("\nDataFrame after dropping missing values:")
print(df_droppedna)

# Adding a newrow
#newrow=[111,'xfg','M',None,8765938474,45000]
#[Link][len(df)] = newrow
#print(df)

# Fill missing values with mean (for numeric columns)


df_filled_mean = [Link](round(df['Age'].mean()))
print("\n Data Frame after filling na values with mean")
print(df_filled_mean)

Output :

Original DataFrame:
Rno Name Gender Age Phone Salary
0 101 aaa M 25.0 2.345678e+09 35000
1 102 bbb F 27.0 5.678938e+09 72000
2 103 ccc M 26.0 6.273458e+09 54000
3 104 ddd M 25.0 7.654938e+09 61000
4 105 eee F 29.0 6.432724e+09 50000
5 106 abc M 26.0 4.563783e+09 62000
6 107 aef F 25.0 6.758493e+09 41000
7 108 srt F NaN 7.658494e+09 33000
8 109 fgh M 25.0 NaN 42000
9 110 yjk M 100.0 5.647383e+09 60000
<class '[Link]'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Rno 10 non-null int64
1 Name 10 non-null object
2 Gender 10 non-null object
3 Age 9 non-null float64
4 Phone 9 non-null float64
5 Salary 10 non-null int64
dtypes: float64(2), int64(2), object(2)
memory usage: 612.0+ bytes
None

Missing values in each column:


Rno 0
Name 0
Gender 0
Age 1
Phone 1
Salary 0
dtype: int64

DataFrame after dropping missing values:


Rno Name Gender Age Phone Salary
0 101 aaa M 25.0 2.345678e+09 35000
1 102 bbb F 27.0 5.678938e+09 72000
2 103 ccc M 26.0 6.273458e+09 54000
3 104 ddd M 25.0 7.654938e+09 61000
4 105 eee F 29.0 6.432724e+09 50000
5 106 abc M 26.0 4.563783e+09 62000
6 107 aef F 25.0 6.758493e+09 41000
9 110 yjk M 100.0 5.647383e+09 60000

Data Frame after filling na values with mean


Rno Name Gender Age Phone Salary
0 101 aaa M 25.0 2.345678e+09 35000
1 102 bbb F 27.0 5.678938e+09 72000
2 103 ccc M 26.0 6.273458e+09 54000
3 104 ddd M 25.0 7.654938e+09 61000
4 105 eee F 29.0 6.432724e+09 50000
5 106 abc M 26.0 4.563783e+09 62000
6 107 aef F 25.0 6.758493e+09 41000
7 108 srt F 34.0 7.658494e+09 33000
8 109 fgh M 25.0 3.400000e+01 42000
9 110 yjk M 100.0 5.647383e+09 60000

# B. Transform Data Using `apply()` and `map()`


import pandas as pd
df=pd.read_csv("[Link]")
print(df)

# Transform using a function


df['Age'] = df['Age'].apply(lambda x: x * 2)
print("\n Data Frame after apply function")
print(df)

# Transform using mapping


if 'Gender' in [Link]:
mapping = {'M': 'Male', 'F': 'Female'} # Example mapping
df['Gender'] = df['Gender'].map(mapping)
print("\n Data Frame after mapping")
print(df)

#replace a value
df['Name'] = df['Name'].replace('aaa', 'AAA')

# Replace multiple values using a dictionary


df['Name'] = df['Name'].replace({'bbb': 'BBB', 'ccc': 'CCC'})
print("\n Data Frame after replace function")
print(df)

Output :

Data Frame after apply function


Rno Name Gender Age Phone Salary
0 101 aaa M 50.0 2.345678e+09 35000
1 102 bbb F 54.0 5.678938e+09 72000
2 103 ccc M 52.0 6.273458e+09 54000
3 104 ddd M 50.0 7.654938e+09 61000
4 105 eee F 58.0 6.432724e+09 50000
5 106 abc M 52.0 4.563783e+09 62000
6 107 aef F 50.0 6.758493e+09 41000
7 108 srt F NaN 7.658494e+09 33000
8 109 fgh M 50.0 NaN 42000
9 110 yjk M 200.0 5.647383e+09 60000

Data Frame after mapping


Rno Name Gender Age Phone Salary
0 101 aaa Male 50.0 2.345678e+09 35000
1 102 bbb Female 54.0 5.678938e+09 72000
2 103 ccc Male 52.0 6.273458e+09 54000
3 104 ddd Male 50.0 7.654938e+09 61000
4 105 eee Female 58.0 6.432724e+09 50000
5 106 abc Male 52.0 4.563783e+09 62000
6 107 aef Female 50.0 6.758493e+09 41000
7 108 srt Female NaN 7.658494e+09 33000
8 109 fgh Male 50.0 NaN 42000
9 110 yjk Male 200.0 5.647383e+09 60000

Data Frame after replace function


Rno Name Gender Age Phone Salary
0 101 AAA Male 50.0 2.345678e+09 35000
1 102 BBB Female 54.0 5.678938e+09 72000
2 103 CCC Male 52.0 6.273458e+09 54000
3 104 ddd Male 50.0 7.654938e+09 61000
4 105 eee Female 58.0 6.432724e+09 50000
5 106 abc Male 52.0 4.563783e+09 62000
6 107 aef Female 50.0 6.758493e+09 41000
7 108 srt Female NaN 7.658494e+09 33000
8 109 fgh Male 50.0 NaN 42000
9 110 yjk Male 200.0 5.647383e+09 60000

# C. Detect and Filter Outliers


import pandas as pd
import numpy as np
from scipy import stats

df = [Link]({'Age': [10, 12, 12, 13, 12, 11, 100]}) # 100 is an outlier
z_scores = [Link]([Link](df['Age']))
#z_scores = (df[‘Age’] - df['Age'].mean()) / df['Age'].std() # Can apply the formula
directly

threshold = 2
outliers = [Link](z_scores > threshold)
print(z_scores)
print("Outliers at positions:", outliers)

df_no_outliers = df[(z_scores < threshold) & (z_scores > threshold)]


print("\nDataFrame after filtering outliers:")
print(df_no_outliers)

Output :

[0.46198371 0.39730599 0.39730599 0.36496713 0.39730599 0.42964485


2.44851367]
Outliers at positions: (array([6]),)

DataFrame after filtering outliers:


Empty DataFrame
Columns: [Age]
Index: []

# D. Perform Vectorized String Operations on Pandas Series


# data without None Values
data = ['peter', 'Paul', 'MARY', 'gUIDO']
[[Link]() for s in data]
# data with possible None Values
names = [Link](data)
print("String Capitalized\n",[Link]())
print("String Length\n",[Link]())
Output :

String Capitalized
0 Peter
1 Paul
2 Mary
3 Guido
dtype: object
String Length
0 5
1 4
2 4
3 5
dtype: int64

# E. Visualize Data
# LINE PLOT:
import pandas as pd
import [Link] as plt
data = {'Year': [2000, 2001, 2002, 2003],'Unemployment Rate': [4.0, 4.7, 5.8, 6.0]}
df = [Link](data)
# Plotting a line chart
[Link](x='Year', y='Unemployment Rate', kind='line')
[Link]()
# BAR PLOT:
df=pd.read_csv(‘[Link]’)

ax = [Link](kind='bar',
x='name',
y='physics_marks',
color='green',
title='BarPlot')
# Customize axis labels (optional, but can be added via 'ax' for more control)
ax.set_xlabel('Name')
ax.set_ylabel('Physics Marks')
Output :

# HISTOGRAM:

import pandas as pd
values = [Link]({
'Length': [2.7, 8.7, 3.4, 2.4, 1.9],
'Breadth': [4.24, 2.67, 7.6, 7.1, 4.9]
})
hist = [Link](bins=5)
[Link]()
Output :

DENSITY PLOT

import pandas as pd
import seaborn as sns
import [Link] as plt
# loading the dataset from seaborn library
data = sns.load_dataset('car_crashes')
# plotting the density plot for 'speeding' attribute using [Link]()
[Link](color='green')
[Link]('Density plot for Speeding')
[Link]()
# viewing the dataset
print([Link](4))
Output :

# SCATTER PLOT

ax = [Link](kind='scatter', x='math_marks', y='physics_marks', color='red',


title='ScatterPlot')

# Customizing plot elements


ax.set_xlabel("Math Marks")

ax.set_ylabel("Physics Marks")
[Link]()

Output :

You might also like