0% found this document useful (0 votes)
7 views18 pages

Data Science Lab Manual: Python Guide

The document is a lab manual for a Data Science course using Python at SRM Institute of Science and Technology. It includes exercises on data analysis, Python programming, and the use of libraries like NumPy, Pandas, and Matplotlib. Each exercise outlines objectives, procedures, and expected outcomes to enhance students' understanding of data science concepts and techniques.

Uploaded by

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

Data Science Lab Manual: Python Guide

The document is a lab manual for a Data Science course using Python at SRM Institute of Science and Technology. It includes exercises on data analysis, Python programming, and the use of libraries like NumPy, Pandas, and Matplotlib. Each exercise outlines objectives, procedures, and expected outcomes to enhance students' understanding of data science concepts and techniques.

Uploaded by

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

SRM Institute of Science and Technology

Ramapuram

Faculty of Science & Humanities

Department of Cybersecurity

LAB MANUAL

USC23601J -
DATA SCIENCE USING PYTHON PROGRAMMING

SEMESTER / YEAR: VI / III

REGULATIONS : 2023

ACADEMIC YEAR: 2025-2026 (Even Semester)

Prepared by [Link]
Approved by [Link] Tamilselvi
INDEX

[Link] Program Title

1 Perform Analysis on Simple Dataset I for Data Science

2 Install Python IDE and perform basic python programs

3 Study of Basic Data Science Libraries in Python

4 Execute python program in Notebook Environment

5 List Methods for Inserting Elements

6 Apply all basic python Dictionary and Sets

7 Manipulation of NumPy Arrays-Indexing, Slicing, Reshaping, Joining and Splitting

8 Perform SciPy operations

9 Implement Data Frame in Pandas

10 Perfom operations on Interactive Data Frames using Python

11 Manipulations on Data Frames using Python

12 Import matpotlib and seaborn Explore a Sample Dataset with it

13 Perform data transformations using Scikit-learn

14 Perform Text Processing using NLTK

15 Implement Machine Learning using Pyhton

CONTENT BEYOND SYLLABUS

1. A Simple program using Pandas for Data Analysis

2. A Simple program using Matplotlib for drawing a graph

3. A Simple program using Scikit learn for classification


4. A Simple program to perform Linear Regression

A Simple program using seaborn to create a heatmap for visualizing correlations in


5.
a dataset
Exercise 1: Perform Analysis on Simple Dataset I for Data Science

AIM:

To understand basic data science operations such as data import, summary statistics,
visualization, and data interpretation using a simple dataset relevant to business intelligence.

DATASET URL:

PROGRAM:

import pandas as pd
# Read the CSV file into a DataFrame

df=pd.read_csv('D:\Lab\Salary_Data.csv')
# Display the first and last few rows of the DataFrame

print([Link]())
print("Last 5 rows of the DataFrame:")
print([Link]())
# 3. Perform a simple operation (e.g., calculate average salary)

average_salary = df['Salary'].mean()
print(f"\nAverage Salary: ${average_salary.2f}")
# 4. Filter data (e.g., find people older than 50)

older_than_50 = df[df['Age'] > 50]


print("\nPeople older than 50:")
print(older_than_50)
# Filter the DataFrame to display rows where 'Salary' is greater than 100000
high_salary_df = df[df['Salary'] > 200000]
print("\nEmployees with Salary > 200000:")
print(high_salary_df)

OUTPUT:

C:\Users\ADMIN\PyCharmMiscProject\.venv\Scripts\[Link] C:\Users\ADMIN\
PyCharmMiscProject\[Link]

df=pd.read_csv('D:\Lab\Salary_Data.csv')

Age Gender ... Years of Experience Salary

0 32.0 Male ... 5.0 90000.0

1 28.0 Female ... 3.0 65000.0

2 45.0 Male ... 15.0 150000.0

3 36.0 Female ... 7.0 60000.0

4 52.0 Male ... 20.0 200000.0

[5 rows x 6 columns]

Last 5 rows of the DataFrame:

Age Gender ... Years of Experience Salary

6699 49.0 Female ... 20.0 200000.0

6700 32.0 Male ... 3.0 50000.0

6701 30.0 Female ... 4.0 55000.0

6702 46.0 Male ... 14.0 140000.0

6703 26.0 Female ... 1.0 35000.0

[5 rows x 6 columns]

Average Salary: $115326.96


People older than 50:

Age Gender ... Years of Experience Salary

4 52.0 Male ... 20.0 200000.0

19 51.0 Male ... 22.0 180000.0

50 51.0 Female ... 22.0 130000.0

60 51.0 Female ... 23.0 170000.0

83 52.0 Male ... 24.0 250000.0

... ... ... ... ... ...

6641 51.0 Female ... 19.0 190000.0

6655 51.0 Female ... 19.0 190000.0

6669 51.0 Female ... 19.0 190000.0

6683 51.0 Female ... 19.0 190000.0

6697 51.0 Female ... 19.0 190000.0

[189 rows x 6 columns]

Employees with Salary > 200000:

Age Gender ... Years of Experience Salary

30 50.0 Male ... 25.0 250000.0

83 52.0 Male ... 24.0 250000.0

105 44.0 Male ... 16.0 220000.0

1531 56.0 Female ... 18.0 210000.0

1655 55.0 Male ... 18.0 210000.0


... ... ... ... ... ...

6200 40.0 Female ... 16.0 215000.0

6214 40.0 Female ... 16.0 215000.0

6228 40.0 Female ... 16.0 215000.0

6242 40.0 Female ... 16.0 215000.0

6256 40.0 Female ... 16.0 215000.0

[114 rows x 6 columns]

Process finished with exit code 0

RESULT:
The program has been executed successfully and business sales metrics were analysed.
Exercise 2: Install Python IDE and perform basic python programs

Objectives :

1. To install the Python interpreter and a suitable IDE (e.g., IDLE or PyCharm Community
Edition).
2. To learn how to write, save, and execute a Python program in both interactive and script
modes.
3. To write basic Python programs using fundamental programming constructs.

Installation and Setup

Install the Python Interpreter:

1. Navigate to the official Python website.


2. Download the latest version of the Python installer for your operating system
(Windows, macOS, or Linux).
3. Run the downloaded executable file.
4. Crucial Step for Windows Users: Ensure you check the box that says "Add Python
to PATH" during the installation process. This allows you to run Python from any
command prompt.
5. Click "Install Now" and follow the on-screen instructions to complete the
installation.
6. Verify the Installation:
7. Open your system's command prompt (Windows: search cmd; macOS/Linux: open
Terminal).
8. Type the command python --version (or python3 --version on some systems) and
press Enter.
9. The installed Python version should be displayed, confirming a successful
installation.
Explore the Default IDE (IDLE):
1. Python comes bundled with a basic IDE called IDLE (Integrated Development and
Learning Environment).
2. Search for "IDLE" in your applications or Start menu and open it. This is your
Python Shell in interactive mode.

Program with User Input and Arithmetic Operations

1. Create a new file in your IDE and enter the following code:

# Program to add two numbers


num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")

# Convert inputs to integers and calculate sum


sum_result = int(num1) + int(num2)

# Display the result


print(f"The sum of {num1} and {num2} is: {sum_result}")
Output:
Enter the first number: 5
Enter the second number: 10
The sum of 5 and 10 is: 15
RESULT:
The program has been executed successfully in Python IDLE environment.
Exercise 3: Study of Basic Data Science Libraries in Python

Aim

To familiarize with and explore the basic features of NumPy, Pandas, and Matplotlib libraries in
Python for data analysis and scientific computation.

Prerequisites

Python installed on the system.

Required libraries installed using pip install numpy pandas matplotlib scikit-learn .

Procedure
I. NumPy (Numerical Python)

NumPy is used for high-performance multi-dimensional array manipulation and numerical


operations.

1. Import the library:

import numpy as np
2. Create a NumPy array and perform basic operations:

 Create a 1D array: arr_1d = [Link]([1, 2, 3, 4, 5])


 Create a 2D array: arr_2d = [Link]([[1, 2, 3], [4, 5, 6]])
 Find the shape and data type: print(arr_2d.shape) and print(arr_2d.dtype)
 Perform an arithmetic operation (e.g., multiplication): print(arr_1d * 2)
 Calculate mean and median: print([Link](arr_1d)) and print([Link](arr_1d))

II. Pandas (Data Analysis and Manipulation)


Pandas is essential for structured data operations, providing DataFrames for efficient handling of
data.

1. Import the library:

import pandas as pd
2. Create a DataFrame from a dictionary and perform manipulations:

Create a simple DataFrame:

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['New York', 'Los Angeles',
'Chicago']}
df = [Link](data)
print(df)
 Select a single column: print(df['Name'])
 Filter rows based on a condition: print(df[df['Age'] > 28])

Load and explore a real-world dataset (e.g., the Iris dataset, available through scikit-learn ):

from [Link] import load_iris


iris = load_iris()
iris_df = [Link](data=[Link], columns=iris.feature_names)
print(iris_df.head()) # Display the first 5 rows
print(iris_df.info()) # Get information about the DataFrame
1. Calculate descriptive statistics: print(iris_df.describe())

Part 3: Matplotlib (Data Visualization)


Matplotlib is the foundation for creating visualizations in Python.

1. Import the plotting module:

import [Link] as plt


2. Create basic visualizations using the Iris DataFrame:

Create a histogram of a specific feature:

[Link](iris_df['sepal length (cm)'], bins=10)


[Link]('Sepal Length Distribution')
[Link]('Sepal Length (cm)')
[Link]('Frequency')
[Link]()
Create a scatter plot of two features:

[Link](iris_df['sepal length (cm)'], iris_df['petal length (cm)'])


[Link]('Sepal Length vs Petal Length')
[Link]('Sepal Length (cm)')
[Link]('Petal Length (cm)')
[Link]()
Optional: Explore other plot types like box plots or density plots.

Expected Outcomes

Upon completion, users will be able to:

1. Understand and implement basic operations using NumPy arrays.

2. Manipulate and analyze structured data using Pandas DataFrames.

3. Create simple yet informative data visualizations using Matplotlib.

Exercise 4: Execute python program in Notebook Environment

AIM:
To perform mathematical, numerical, and data engineering analysis on a small temperature
dataset using Python.

INTRODUCTION:
Data analysis involves several key steps:
 Mathematical analysis calculates statistical properties like mean, median, and standard
deviation.
 Numerical processing involves computation, such as unit conversion and smoothing.
 Data engineering includes loading, cleaning, and transforming data for further use.
In this program, we analyze a week's worth of temperature data to demonstrate these processes.

PROGRAM:
import numpy as np
import pandas as pd

# Sample temperature data in Celsius


data = { 'Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
'Temp_C': [22.1, 23.5, 21.0, 25.3, None, 26.1, 24.8] }

# Step 1: Load the data (Data Engineering)


df = [Link](data)

# Step 2: Clean the data (fill missing values with the mean)
df['Temp_C'] = df['Temp_C'].fillna(df['Temp_C'].mean())

# Step 3: Mathematical Analysis


mean_temp = df['Temp_C'].mean()
median_temp = df['Temp_C'].median()
std_temp = df['Temp_C'].std()

# Step 4: Numerical Processing


df['Temp_F'] = df['Temp_C'] * 9/5 + 32 # Convert Celsius to Fahrenheit
df['Smoothed_C'] = df['Temp_C'].rolling(window=3, min_periods=1).mean()
# Rolling average

# Step 5: Output results


print("Mathematical Summary:")
print(f"Mean Temperature: {mean_temp:.2f} °C")
print(f"Median Temperature: {median_temp:.2f} °C")
print(f"Standard Deviation: {std_temp:.2f} °C\n")
print("Processed Temperature Data:\n")
print(df)

OUTPUT:
Mathematical Summary:
Mean Temperature: 23.80 °C
Median Temperature: 23.50 °C
Standard Deviation: 1.80 °C

Processed Temperature Data:

Day Temp_C Temp_F Smoothed_C


0 Mon 22.1 71.78 22.10
1 Tue 23.5 74.30 22.80
2 Wed 21.0 69.80 22.20
3 Thu 25.3 77.54 23.27
4 Fri 23.8 74.84 23.37
5 Sat 26.1 79.00 25.07
6 Sun 24.8 76.64 24.90

RESULT:
 The program successfully calculated statistical metrics:
o Mean = 23.80°C
o Median = 23.50°C
o Standard Deviation = 1.80°C
 Missing temperature data was filled using the average.
 Data was numerically transformed by:
o Converting °C to °F
o Smoothing with a rolling average
 Final output shows a clean, enriched dataset ready for further analysis or visualization.
Exercise 5: List Methods for Inserting Elements

AIM:
To demonstrate various Python list methods for adding elements such as
append(), insert(), extend() and slice().
PROGRAM:
# Initialize a sample list
my_list = [1, 2, 3, 4, 5]

# 1. insert() - Insert element at a specified index


my_list.insert(2, 10) # Insert 10 at index 2 (3rd position)
print("After insert(2, 10):", my_list)

# 2. append() - Add element to the end of the list


my_list.append(20)
print("After append(20):", my_list)

# 3. extend() - Add multiple elements to the end of the list


my_list.extend([30, 40])
print("After extend([30, 40]):", my_list)

# 4. Using + operator to concatenate lists (creates a new list)


new_list = my_list + [50, 60]
print("After + operator:", new_list)

# 5. Using slice assignment to insert multiple elements at a specific position


my_list[3:3] = [100, 200] # Insert 100,200 starting at index 3
print("After slice assignment at index 3:", my_list)

OUTPUT:
After insert(2, 10): [1, 2, 10, 3, 4, 5]
After append(20): [1, 2, 10, 3, 4, 5, 20]
After extend([30, 40]): [1, 2, 10, 3, 4, 5, 20, 30, 40]
After + operator: [1, 2, 10, 3, 4, 5, 20, 30, 40, 50, 60]
After slice assignment at index 3: [1, 2, 10, 100, 200, 3, 4, 5, 20, 30, 40]

RESULT:
Thus the program has been executed successfully and the output is verified.

Simple Programs:
[Link] a excel file and read the file using Jupyter Notebook

import pandas as pd
# Read the Excel file into a DataFrame
df = pd.read_excel('[Link]')
# Print the first 5 rows of the DataFrame
[Link] = ['Name','age','Stream','Percentage']
print([Link]())
print([Link])
print([Link])
print(len(df))

Name Age Stream Percentage


Ankit 18 Math 95
Rahul 19 Science 90
Shaurya 20 Commerce 85
Aishwarya 18 Math 80
Priyanka 19 Science 75
2. Generate squares of numbers using random variable - Jupyter Notebook

import numpy as np
def square(x):
return x * x
x = [Link](1, 10)
y = square(x)
print('%d squared is %d' % (x, y))
print('%d squared is %d' % (x, y))

# Program to perform operations based on user input

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))

print("Sum =", x + y)
print("Difference =", x - y)
print("Product =", x * y)
print("Quotient =", x / y)
print("Remainder =", x % y)

You might also like