Data Analysis with Python Lab
TUMKUR UNIVERSITY
UNIVERSITY COLLEGE OF SCIENCE
TUMKUR
DEPARTMENT OF COMPUTER SCIENCE
2ND YEAR 4TH SEM BCA-DS
SUBJECT: DATA ANALYSIS WITH PYTHON
Submitted by: Kanyakumari B
KanyaKumari B (Dept of CS) UCST Page 1
Data Analysis with Python Lab
LIST OF PROGRAMS:
1: Python Refresher Write a Python program to:
a) Create a list, tuple, dictionary and perform basic operations (add, delete,
update).
b) Define a function to compute factorial of a number using recursion.
c) Read and write a text file using file handling.
2: NumPy Basics Write a program to:
a) Create NumPy arrays and perform slicing, indexing, and reshaping.
b) Perform element-wise operations (addition, multiplication) on arrays.
c) Use NumPy to calculate mean, median, standard deviation of a dataset.
3: Working with Pandas – DataFrames
Using the Titanic dataset or any sample dataset:
a) Load the dataset using pd.read_csv()
b) Display basic information: shape, columns, data types.
c) Filter rows where Age > 30.
d) Sort the data based on Fare in descending order.
4: Data Cleaning and Missing Values Using a dataset with missing values:
a) Identify missing values using isnull()
b) Fill missing values with mean/median.
c) Drop columns or rows with too many null values.
d) Detect and handle duplicate rows.
5: Data Transformation and Encoding
Using a dataset with categorical variables:
a) Perform label encoding and one-hot encoding.
b) Normalize/standardize numerical columns.
c) Combine multiple datasets using merge and join operations.
6: Data Visualization with Matplotlib Write a program to:
a) Plot a line chart, bar chart, histogram, and scatter plot using Matplotlib.
b) Customize plots: add title, labels, legends, grid, and color.
7: Data Visualization with Seaborn
Using the Iris dataset or Titanic:
a) Create a boxplot of sepal length by species.
b) Plot a heatmap of correlation matrix.
c) Generate pairplot for all numeric variables.
d) Customize plot aesthetics using Seaborn themes.
KanyaKumari B (Dept of CS) UCST Page 2
Data Analysis with Python Lab
8: Descriptive Statistics Write a program to compute:
a) Mean, median, mode, variance, standard deviation of a dataset.
b) Correlation between two numeric columns.
c) Covariance matrix of a DataFrame.
9: Hypothesis Testing Using a sample or real dataset:
a) Perform a one-sample t-test to compare mean of a column to a fixed value.
b) Perform a two-sample t-test between two groups (e.g., male vs. female fares).
c) Apply chi-square test for independence between two categorical columns.
10: Exploratory Data Analysis (EDA) Project
Choose any dataset (from Kaggle, UCI, etc.) and perform:
a) Data loading and inspection
b) Data cleaning and preprocessing
c) Visual exploration of features
d) Statistical summary and correlation analysis
e) Write a summary report or present findings
Sample Datasets to be used
Titanic Dataset ([Link])
Iris Dataset ([Link])
COVID-19 Dataset
Sales Data
Superstore Dataset
Guidelines
All lab exercises must be submitted as Jupyter Notebooks (.ipynb) or .py files.
Include output screenshots for each task.
Final mini-project to be submitted as a report + code notebook.
KanyaKumari B (Dept of CS) UCST Page 3
Data Analysis with Python Lab
1: Python Refresher
Write a Python program to:
a) Create a list, tuple, dictionary and perform basic operations (add, delete,
update).
# Create a list
my_list = [1, 2, 3]
print("Original List:", my_list)
# Add
my_list.append(4)
print("After Add:", my_list)
# Update
my_list[0] = 10
print("After Update:", my_list)
# Delete
my_list.remove(2)
print("After Delete:", my_list)
Original List: [1, 2, 3]
After Add: [1, 2, 3, 4]
After Update: [10, 2, 3, 4]
After Delete: [10, 3, 4]
# Tuple
my_tuple = (5, 6, 7)
print("\nOriginal Tuple:", my_tuple)
# Tuple is immutable, so convert to list
temp = list(my_tuple)
# Add
[Link](8)
# Update
temp[1] = 60
# Delete
[Link](7)
my_tuple = tuple(temp)
print("Updated Tuple:", my_tuple)
KanyaKumari B (Dept of CS) UCST Page 4
Data Analysis with Python Lab
Original Tuple: (5, 6, 7)
Updated Tuple: (5, 60, 8)
# Create a dictionary
my_dict = {"name": "John", "age": 20}
print("Original Dictionary:", my_dict)
# Add
my_dict["city"] = "Delhi"
# Update
my_dict["age"] = 21
# Delete
del my_dict["name"]
print("Updated Dictionary:", my_dict)
Original Dictionary: {'name': 'John', 'age': 20}
Updated Dictionary: {'age': 21, 'city': 'Delhi'}
b) Define a function to compute factorial of a number using recursion.
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
Enter a number: 5
Factorial of 5 is 120
c) Read and write a text file using file handling.
# Writing to a file
file = open("[Link]", "w")
[Link]("File operations in Python.")
[Link]()
# Reading from a file
file = open("[Link]", "r")
content = [Link]()
[Link]()
KanyaKumari B (Dept of CS) UCST Page 5
Data Analysis with Python Lab
print("\nFile Content:")
print(content)
File Content:
File operations in Python.
KanyaKumari B (Dept of CS) UCST Page 6
Data Analysis with Python Lab
2: NumPy Basics : Write a program to:
a) Create NumPy arrays and perform slicing, indexing, and reshaping.
import numpy as np
# 1. Create NumPy arrays
arr1 = [Link]([1, 2, 3, 4, 5, 6])
arr2 = [Link]([10, 20, 30, 40, 50, 60])
print("Array 1:", arr1)
print("Array 2:", arr2)
# Slicing and Indexing
print("\nSlicing :", arr1[1:5])
print("Indexing :", arr1[2])
# Reshaping
reshaped_arr = [Link](2, 3)
print("\nReshaped Array is :\n", reshaped_arr)
Array 1: [1 2 3 4 5 6]
Array 2: [10 20 30 40 50 60]
Slicing : [2 3 4 5]
Indexing : 3
b) Perform element-wise operations (addition, multiplication) on arrays.
# Element-wise operations
addition = arr1 + arr2
multiplication = arr1 * arr2
print("\nElement-wise Addition:", addition)
print("Element-wise Multiplication:", multiplication)
Element-wise Addition: [11 22 33 44 55 66]
Element-wise Multiplication: [ 10 40 90 160 250 360]
c) Use NumPy to calculate mean, median, standard deviation of a dataset.
# Statistical calculations
data = [Link]([12, 15, 18, 20, 22, 25, 30])
mean = [Link](data)
median = [Link](data)
std_dev = [Link](data)
print("\nDataset:", data)
print("Mean:", mean)
print("Median:", median)
print("Standard Deviation:", std_dev)
KanyaKumari B (Dept of CS) UCST Page 7
Data Analysis with Python Lab
Dataset: [12 15 18 20 22 25 30]
Mean: 20.285714285714285
Median: 20.0
Standard Deviation: 5.624291338579865
KanyaKumari B (Dept of CS) UCST Page 8
Data Analysis with Python Lab
3: Working with Pandas – DataFrames
Using the Titanic dataset or any sample dataset:
a) Load the dataset using pd.read_csv()
b) Display basic information: shape, columns, data types.
c) Filter rows where Age > 30.
d) Sort the data based on Fare in descending order.
import pandas as pd
1) Load the dataset
df = pd.read_csv("C:/Users/HP/Downloads/[Link]")
a) Shape of the dataset
shape_df = [Link]({
"Description": ["Rows", "Columns"],
"Value": [[Link][0], [Link][1]]
})
display(shape_df)
b) Display basic information
# Display column names with data types
col_dtype_df = [Link]({
"Column Name": [Link],
"Data Type": [Link]
})
display(col_dtype_df)
c) Filter rows where Age > 30
age_above_30 = df[df["Age"] > 30]
print("\nPassengers with Age > 30:")
display(age_above_30.head())
d) Sort the data based on Fare in descending order
sorted_by_fare = df.sort_values(by="Fare", ascending=False)
print("\nData sorted by Fare (descending):")
display(sorted_by_fare.head())
KanyaKumari B (Dept of CS) UCST Page 9
Data Analysis with Python Lab
KanyaKumari B (Dept of CS) UCST Page 10
Data Analysis with Python Lab
4: Data Cleaning and Missing Values
Using a dataset with missing values:
1) Identify missing values using isnull()
import pandas as pd
import numpy as np
data = {
'Student Name': ['chandu', 'abhi', 'darshu', 'chethu'],
'Marks': [85, [Link], 90, 85],
'Age': [20, 21, [Link], 20]
}
df = [Link](data)
display(df)
2) Fill missing values with mean/median.
Fill with mean:
df['Marks'] = df['Marks'].fillna(df['Marks'].mean())
print("fill missing values with mean")
display(df)
Fill with median:
df['Age'] = df['Age'].fillna(df['Age'].median())
print("fill missing values with median")
display(df)
KanyaKumari B (Dept of CS) UCST Page 11
Data Analysis with Python Lab
3) Drop columns or rows with too many null values.
Drop rows:
[Link]()
print("drop rows")
display(df)
Drop columns:
[Link](axis=1)
print("drop columns")
display(df)
4) Detect and handle duplicate row
Detect duplicates:
[Link]()
print("detect duplicate elements")
display(df)
KanyaKumari B (Dept of CS) UCST Page 12
Data Analysis with Python Lab
Handle duplicates:
df = df.drop_duplicates()
print("Handle duplicate elements")
display(df)
KanyaKumari B (Dept of CS) UCST Page 13
Data Analysis with Python Lab
5: Data Transformation and Encoding
Using a dataset with categorical variables:
a) Perform label encoding and one-hot encoding.
import pandas as pd
# Create a dataset
data = {
'Name': ['Asha', 'Ravi', 'Meena', 'Kiran'],
'Gender': ['Female', 'Male', 'Female', 'Male'],
'City': ['Bangalore', 'Mysore', 'Bangalore', 'Mysore'],
'Age': [20, 22, 21, 23],
'Salary': [20000, 25000, 22000, 27000]
}
df = [Link](data)
display(df)
from [Link] import LabelEncoder
le = LabelEncoder()
df['Gender_Label'] = le.fit_transform(df['Gender'])
print(df[['Gender', 'Gender_Label']])
df_onehot = pd.get_dummies(df, columns=['City'])
print(df_onehot)
b) Normalize/standardize numerical columns.
from [Link] import MinMaxScaler
scaler = MinMaxScaler()
df[['Age_Norm', 'Salary_Norm']] = scaler.fit_transform(df[['Age', 'Salary']])
print(df[['Age', 'Age_Norm', 'Salary', 'Salary_Norm']])
KanyaKumari B (Dept of CS) UCST Page 14
Data Analysis with Python Lab
from [Link] import StandardScaler
std = StandardScaler()
df[['Age_Std', 'Salary_Std']] = std.fit_transform(df[['Age', 'Salary']])
print(df[['Age_Std', 'Salary_Std']])
c) Combine multiple datasets using merge and join operations.
dept = [Link]({
'Name': ['Asha', 'Ravi', 'Meena', 'Kiran'],
'Department': ['IT', 'HR', 'Finance', 'IT']
})
print(dept)
merged_df = [Link](df, dept, on='Name')
print(merged_df[['Name', 'Department', 'Salary']])
df_join = df.set_index('Name').join(dept.set_index('Name'))
print(df_join[['Department', 'Salary']])
KanyaKumari B (Dept of CS) UCST Page 15
Data Analysis with Python Lab
6: Data Visualization with Matplotlib. Write a program to:
a) Plot a line chart, bar chart, histogram, and scatter plot using Matplotlib.
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [10, 15, 20, 25, 30]
Line Chart:
[Link]()
[Link](x, y, color='green', marker='o', label='Line chart')
[Link]("Line Chart Example")
[Link]("X Values")
[Link]("Y Values")
[Link]()
[Link](True)
[Link]()
Bar Chart
categories = ['A', 'B', 'C', 'D']
values = [20, 35, 30, 25]
[Link]()
[Link](categories, values, color='green', label='Bar Chart')
[Link]("Bar Chart Example")
[Link]("Categories")
[Link]("Values")
[Link]()
[Link](axis='y')
[Link]()
KanyaKumari B (Dept of CS) UCST Page 16
Data Analysis with Python Lab
Histogram
data = [10, 20, 20, 30, 30, 30, 40, 40, 50]
[Link]()
[Link](data, bins=5, color='orange', edgecolor='black', label='Histogram')
[Link]("Histogram Example")
[Link]("Data Values")
[Link]("Frequency")
[Link]()
[Link](True)
[Link]()
Scatter Plot
x_scatter = [1, 2, 3, 4, 5]
y_scatter = [5, 15, 10, 25, 20]
[Link]()
[Link](x_scatter, y_scatter, color='red', label='Scatter Plot')
[Link]("Scatter Plot Example")
[Link]("X Axis")
KanyaKumari B (Dept of CS) UCST Page 17
Data Analysis with Python Lab
[Link]("Y Axis")
[Link]()
[Link](True)
[Link]()
b) Customize plots: add title, labels, legends, grid, and color.
import [Link] as plt
# Data
subjects = ['Machine Learning', 'Artificial intelligence', 'Data Analysis']
passed = [45, 40, 48]
failed = [5, 10, 2]
# Plot
[Link](subjects, passed, color='green', marker='o', label='Passed')
[Link](subjects, failed, color='red', marker='x', label='Failed')
# Customize
[Link]("Student Result Analysis 2026")
[Link]("Subjects")
[Link]("Number of Students")
[Link]()
[Link](True)
# Show plot
[Link]()
KanyaKumari B (Dept of CS) UCST Page 18
Data Analysis with Python Lab
KanyaKumari B (Dept of CS) UCST Page 19
Data Analysis with Python Lab
7: Data Visualization with Seaborn
Using the Iris dataset or Titanic:
# Import required libraries
import seaborn as sns
import [Link] as plt
import pandas as pd
# Load Iris dataset
iris = sns.load_dataset("iris")
a) Create a boxplot of sepal length by species.
[Link](figsize=(6,4))
[Link](x="species", y="sepal_length", data=iris)
[Link]("Boxplot of Sepal Length by Species")
[Link]()
b) Plot a heatmap of correlation matrix.
corr = [Link](numeric_only=True)
[Link](figsize=(6,4))
[Link](corr, annot=True, cmap="coolwarm")
[Link]("Correlation Heatmap")
[Link]()
KanyaKumari B (Dept of CS) UCST Page 20
Data Analysis with Python Lab
c) Generate pairplot for all numeric variables.
[Link](iris, hue="species")
[Link]()
KanyaKumari B (Dept of CS) UCST Page 21
Data Analysis with Python Lab
d) Customize plot aesthetics using Seaborn themes.
sns.set_theme(style="darkgrid", palette="Set2")
[Link](figsize=(6,4))
[Link](x="species", y="petal_length", data=iris)
[Link]("Customized Boxplot with Seaborn Theme")
[Link]()
KanyaKumari B (Dept of CS) UCST Page 22
Data Analysis with Python Lab
8: Descriptive Statistics: Write a program to compute:
a) Mean, median, mode, variance, standard deviation of a dataset.
b) Correlation between two numeric columns.
c) Covariance matrix of a DataFrame.
import pandas as pd
from scipy import stats
data = {
"Python": [70, 85, 90, 75, 80, 95],
"PHP": [65, 80, 85, 70, 75, 90]
}
df = [Link](data)
mean= df["Python"].mean()
median = df["Python"].median()
mode = [Link](df["Python"], keepdims=True).mode[0]
variance = df["Python"].var()
std = df["Python"].std()
print("Mean:",mean)
print("Median:",median)
print("Mode:",mode)
print("Variance:",variance)
print("Standard Deviation:",std)
correlation = df["Python"].corr(df["PHP"])
print("\nCorrelation between Python and PHP:", correlation)
cov_matrix = [Link]()
print("\nCovariance Matrix:\n", cov_matrix)
Mean: 82.5
Median: 82.5
Mode: 70
Variance: 87.5
Standard Deviation: 9.354143466934854
Correlation between Python and PHP: 0.9999999999999998
Covariance Matrix:
Python PHP
Python 87.5 87.5
PHP 87.5 87.5
KanyaKumari B (Dept of CS) UCST Page 23
Data Analysis with Python Lab
9: Hypothesis Testing
Using a sample or real dataset:
a) Perform a one-sample t-test to compare mean of a column to a fixed value.
import pandas as pd
from [Link] import ttest_1samp, ttest_ind, chi2_contingency
# Load dataset
df = pd.read_csv("C:/Users/HP/Downloads/[Link]")
# One-sample t-test: compare mean Fare with fixed value 30
t_stat, p_value = ttest_1samp(df["Fare"].dropna(), 30)
print("t-statistic:", t_stat)
print("p-value:", p_value)
t-statistic: 1.3240136368613238
p-value: 0.18583845591428397
b) Perform a two-sample t-test between two groups (e.g., male vs. female fares).
# Separate fares for male and female passengers
male_fares = df[df["Sex"] == "male"]["Fare"].dropna()
female_fares = df[df["Sex"] == "female"]["Fare"].dropna()
# Perform two-sample t-test
t_stat, p_value = ttest_ind(male_fares, female_fares)
print("t-statistic:", t_stat)
print("p-value:", p_value)
t-statistic: -5.529140269385719
p-value: 4.2308678700429995e-08
c) Apply chi-square test for independence between two categorical columns.
# Create a contingency table for two categorical columns
table = [Link](df["Sex"], df["Survived"])
# Perform chi-square test
chi2, p_value, dof, expected = chi2_contingency(table)
print("Chi-square value:", chi2)
print("p-value:", p_value)
Chi-square value: 260.71702016732104
p-value: 1.1973570627755645e-58
KanyaKumari B (Dept of CS) UCST Page 24
Data Analysis with Python Lab
10: Exploratory Data Analysis (EDA) Project
Choose any dataset (from Kaggle, UCI, etc.) and perform:
a) Data loading and inspection
import pandas as pd
import seaborn as sns
import [Link] as plt
# Load dataset
df = sns.load_dataset("iris")
# Display first 5 rows
display([Link]())
# Dataset information
display([Link]())
# Dataset shape
display("Shape:", [Link])
b) Data cleaning and preprocessing
# Check for missing values
print([Link]().sum())
KanyaKumari B (Dept of CS) UCST Page 25
Data Analysis with Python Lab
# Remove duplicates (if any)
df = df.drop_duplicates()
# Encode categorical variable (species)
df['species_encoded'] = df['species'].astype('category').[Link]
display([Link]())
c) Visual exploration of features
# Histogram
[Link](figsize=(8,6))
[Link]()
# Boxplot
[Link](figsize=(6,4))
[Link](data=[Link](columns=['species']))
[Link]()
KanyaKumari B (Dept of CS) UCST Page 26
Data Analysis with Python Lab
d) Statistical summary and correlation analysis
# Statistical summary
display([Link]())
KanyaKumari B (Dept of CS) UCST Page 27
Data Analysis with Python Lab
# Calculate correlation
correlation = [Link](numeric_only=True)
display(correlation)
e) Write a summary report or present findings
EDA SUMMARY REPORT:
1. The Iris dataset contains 150 rows and 5 columns.
2. No missing values were found in the dataset.
3. Sepal and petal measurements vary significantly across species.
4. Petal length and petal width show strong positive correlation.
5. Species can be clearly distinguished using petal features.
KanyaKumari B (Dept of CS) UCST Page 28