Module 1: Data Manipulation and Analysis using
Pandas and NumPy
1. Load a CSV dataset into a Pandas DataFrame. Perform basic
data inspection
such as displaying the first few rows, checking data types,
handling missing values,
and removing duplicate rows.
2. Using Pandas, load a dataset and perform column-wise
operations such as
adding a new calculated column, renaming columns, changing
data types, and
dropping unnecessary columns.
3. Using NumPy, generate a random dataset of 1000 values.
Calculate basic
statistical measures such as mean, median, variance, standard
deviation, minimum,
and maximum using NumPy functions.
4. Write a Pandas program to group a dataset by one or more
categorical
columns and calculate summary statistics such as count, mean,
and standard
deviation for each group.
5. Load a dataset into Pandas and filter rows based on complex
conditions using
.loc and .query(). For example, filter rows where sales are above a
threshold and
region equals "North".
6. Using NumPy, create two matrices (3x3) filled with random
integers. Perform
matrix addition, subtraction, multiplication, element-wise
division, and calculate the
determinant and inverse.
7. Write a Pandas program to merge two DataFrames using
different types of
joins (inner, outer, left, right). Use sample data representing
customer details and
order details.
8. Load a dataset and perform time series analysis using Pandas
DateTime
features. Extract year, month, day, and weekday from a date
column, and group data
by month to calculate monthly sales.
9. Using NumPy, generate a 1D array of 100 random integers
between 1 and
1000. Use Boolean indexing to filter all values greater than 500
and less than 800,
and calculate the mean of filtered values.
10. Write a Pandas program to pivot a DataFrame to create a
pivot table
summarizing the data. Use sample sales data to show product-
wise sales for each
region.
Here are 10 Python programs using Pandas & NumPy with code,
sample output, and explanation—structured for teaching and practical
understanding.
1. Load CSV & Basic Data Inspection
Code
import pandas as pd
df = pd.read_csv("[Link]")
print([Link]())
print([Link]())
print([Link]().sum())
df = df.drop_duplicates()
df = [Link](method='ffill')
print(df)
Sample Output
Name Age Salary
0 A 25 20000
1 B 30 30000
<class '[Link]'>
Age int64
Salary int64
Age 0
Salary 1
Explanation
head() → shows first rows
info() → data types
isnull() → missing values
fillna() → handle missing values
drop_duplicates() → removes duplicate rows
2. Column-wise Operations
Code
import pandas as pd
df = pd.read_csv("[Link]")
df['Bonus'] = df['Salary'] * 0.1
[Link](columns={'Salary': 'Income'}, inplace=True)
df['Age'] = df['Age'].astype(float)
[Link](columns=['Bonus'], inplace=True)
print(df)
Output
Name Age Income
0 A 25.0 20000
Explanation
Add column → calculation
Rename columns
Change datatype
Drop unnecessary columns
3. NumPy Statistics
Code
import numpy as np
data = [Link](1, 100, 1000)
print("Mean:", [Link](data))
print("Median:", [Link](data))
print("Variance:", [Link](data))
print("Std Dev:", [Link](data))
print("Min:", [Link](data))
print("Max:", [Link](data))
Output (Example)
Mean: 50.2
Median: 51
Variance: 820
Std Dev: 28.6
Min: 1
Max: 99
Explanation
NumPy provides fast statistical operations on arrays.
4. GroupBy Operations
Code
import pandas as pd
data = {
'Department': ['IT', 'IT', 'HR', 'HR'],
'Salary': [30000, 40000, 25000, 27000]
df = [Link](data)
grouped = [Link]('Department')['Salary'].agg(['count', 'mean', 'std'])
print(grouped)
Output
count mean std
Department
HR 2 26000 1414
IT 2 35000 7071
Explanation
Groups data by category
Calculates summary statistics
5. Filtering with .loc and .query()
Code
import pandas as pd
df = [Link]({
'Region': ['North', 'South', 'North'],
'Sales': [500, 300, 700]
})
filtered1 = [Link][(df['Sales'] > 400) & (df['Region'] == 'North')]
filtered2 = [Link]("Sales > 400 and Region == 'North'")
print(filtered1)
print(filtered2)
Output
Region Sales
0 North 500
2 North 700
Explanation
.loc → conditional filtering
.query() → SQL-like filtering
6. NumPy Matrix Operations
Code
import numpy as np
A = [Link](1, 10, (3,3))
B = [Link](1, 10, (3,3))
print("Addition:\n", A + B)
print("Subtraction:\n", A - B)
print("Multiplication:\n", [Link](A, B))
print("Division:\n", A / B)
print("Determinant:", [Link](A))
print("Inverse:\n", [Link](A))
Output (Example)
Addition:
[[10 12 9]
...]
Determinant: 45.0
Explanation
Matrix operations using NumPy
Linear algebra functions
7. Merging DataFrames (Joins)
Code
import pandas as pd
customers = [Link]({
'ID': [1,2],
'Name': ['A','B']
})
orders = [Link]({
'ID': [1,3],
'Amount': [500,700]
})
print([Link](customers, orders, how='inner', on='ID'))
print([Link](customers, orders, how='left', on='ID'))
print([Link](customers, orders, how='right', on='ID'))
print([Link](customers, orders, how='outer', on='ID'))
Explanation
Inner → common records
Left → all from left
Right → all from right
Outer → all records
8. Time Series Analysis
Code
import pandas as pd
df = [Link]({
'Date': pd.date_range(start='2024-01-01', periods=5),
'Sales': [100, 200, 150, 300, 250]
})
df['Year'] = df['Date'].[Link]
df['Month'] = df['Date'].[Link]
df['Day'] = df['Date'].[Link]
df['Weekday'] = df['Date'].dt.day_name()
monthly = [Link]('Month')['Sales'].sum()
print(monthly)
Output
Month
1 1000
Explanation
Extract date components
Group by month
9. Boolean Indexing (NumPy)
Code
import numpy as np
arr = [Link](1, 1000, 100)
filtered = arr[(arr > 500) & (arr < 800)]
print("Filtered:", filtered)
print("Mean:", [Link](filtered))
Output (Example)
Filtered: [520 600 750]
Mean: 623.3
Explanation
Boolean indexing filters values
Efficient data selection
10. Pivot Table
Code
import pandas as pd
data = {
'Region': ['North','South','North','South'],
'Product': ['A','A','B','B'],
'Sales': [100,200,150,250]
}
df = [Link](data)
pivot = pd.pivot_table(df, values='Sales',
index='Product',
columns='Region',
aggfunc='sum')
print(pivot)
Output
Region North South
Product
A 100 200
B 150 250
Explanation
Summarizes data
Useful for reporting and dashboards
Module 2: Data Visualization using Seaborn
1. Load a dataset into Pandas and use Seaborn to plot a
histogram showing the
distribution of a numerical column. Customize the bin size, color,
and add a title.
2. Using Seaborn, plot a boxplot for a numerical column grouped
by a categorical
column (e.g., salary distribution across different departments)
from a given
dataset.
3. Load a dataset into Pandas and create a pairplot using Seaborn
to visualize
pairwise relationships between all numerical columns. Add hue to
distinguish
different categories.
4. Create a Seaborn heatmap using a correlation matrix
generated from a
DataFrame. Customize the color palette, annotations, and title.
5. Using Seaborn, create a barplot comparing the average values
of a numerical
column for different categories of a categorical column.
Customize axes labels
and titles.
6. Load a dataset and create a Seaborn scatter plot between two
numerical
columns. Add hue to differentiate categories and customize
markers and plot
size.
7. Using Seaborn, create a line plot to visualize trends in a time
series dataset.
Customize the plot with appropriate labels, grid lines, and title.
8. Load a dataset with multiple numerical columns and create a
Seaborn violin plot
to show the distribution of values for each column grouped by a
categorical
column.
9. Create a Seaborn count plot to visualize the frequency
distribution of values in a
categorical column from a dataset. Customize colors, orientation,
and add value
labels on top of bars.
[Link] a dataset and create a Seaborn facet grid of scatter
plots, showing
relationships between two numerical columns for different values
of a third
categorical column.
Here are 10 Seaborn + Pandas visualization programs with code,
sample output description, and explanation—ideal for
lab/practical teaching.
1. Histogram (Distribution Plot)
Code
import pandas as pd
import seaborn as sns
import [Link] as plt
df = [Link]({'Salary': [20000, 30000, 25000, 40000,
35000]})
[Link](df['Salary'], bins=5, color='blue')
[Link]("Salary Distribution")
[Link]()
Output (Description)
Histogram showing salary distribution with 5 bins
Blue colored bars
Explanation
histplot() → shows frequency distribution
bins → controls grouping
Helps understand spread of data
2. Boxplot (Grouped)
Code
import pandas as pd
import seaborn as sns
import [Link] as plt
df = [Link]({
'Department': ['IT','IT','HR','HR'],
'Salary': [30000, 40000, 25000, 27000]
})
[Link](x='Department', y='Salary', data=df)
[Link]("Salary Distribution by Department")
[Link]()
Output
Boxplot comparing IT vs HR salaries
Explanation
Shows median, quartiles, and outliers
Useful for comparison across categories
3. Pairplot
Code
import seaborn as sns
df = sns.load_dataset('iris')
[Link](df, hue='species')
Output
Multiple scatter plots between all numerical features
Color-coded by species
Explanation
Shows relationships between variables
hue adds category distinction
4. Heatmap (Correlation Matrix)
Code
import pandas as pd
import seaborn as sns
import [Link] as plt
df = sns.load_dataset('iris')
corr = [Link](numeric_only=True)
[Link](corr, annot=True, cmap='coolwarm')
[Link]("Correlation Heatmap")
[Link]()
Output
Colored matrix showing correlations
Explanation
Helps identify strong/weak relationships
annot=True → shows values
5. Barplot (Average Values)
Code
import seaborn as sns
import [Link] as plt
df = sns.load_dataset('tips')
[Link](x='day', y='total_bill', data=df)
[Link]("Day")
[Link]("Average Bill")
[Link]("Average Bill per Day")
[Link]()
Output
Bars showing average bill per day
Explanation
Aggregates mean values automatically
Useful for comparisons
6. Scatter Plot
Code
import seaborn as sns
import [Link] as plt
df = sns.load_dataset('tips')
[Link](x='total_bill', y='tip', hue='sex', style='sex',
data=df)
[Link]("Bill vs Tip")
[Link]()
Output
Scatter plot with different colors and markers
Explanation
Shows relationship between two variables
hue and style differentiate categories
7. Line Plot (Time Series)
Code
import pandas as pd
import seaborn as sns
import [Link] as plt
df = [Link]({
'Date': pd.date_range(start='2024-01-01', periods=5),
'Sales': [100, 200, 150, 300, 250]
})
[Link](x='Date', y='Sales', data=df)
[Link]("Sales Trend")
[Link](True)
[Link]()
Output
Line graph showing trend over time
Explanation
Useful for time-series analysis
Grid improves readability
8. Violin Plot
Code
import seaborn as sns
import [Link] as plt
df = sns.load_dataset('tips')
[Link](x='day', y='total_bill', data=df)
[Link]("Bill Distribution by Day")
[Link]()
Output
Violin shapes showing distribution
Explanation
Combines boxplot + density
Shows distribution shape clearly
9. Count Plot
Code
import seaborn as sns
import [Link] as plt
df = sns.load_dataset('tips')
ax = [Link](x='day', data=df)
for p in [Link]:
[Link](f'{p.get_height()}',
(p.get_x() + p.get_width() / 2., p.get_height()),
ha='center', va='bottom')
[Link]("Count of Records per Day")
[Link]()
Output
Bar chart showing frequency counts
Values displayed on bars
Explanation
Counts categorical occurrences
Annotation improves clarity
10. Facet Grid (Multiple Scatter Plots)
Code
import seaborn as sns
df = sns.load_dataset('tips')
g = [Link](df, col="time")
[Link]([Link], "total_bill", "tip")
Output
Separate scatter plots for Lunch and Dinner
Explanation
Splits data into subsets
Useful for multi-dimensional analysis