0% found this document useful (0 votes)
13 views31 pages

Data Science Lab Assessment Guide

Data science with python

Uploaded by

Aviral Sharma
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)
13 views31 pages

Data Science Lab Assessment Guide

Data science with python

Uploaded by

Aviral Sharma
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

DATA SCIENCE LAB

Paper Code: CIE-405P

Faculty Name: Student Name:


Prof. (Dr)Bhoomi Gupta Roll No:
(HOD, ITE) Semester:
Group:

Maharaja Agrasen Institute of Technology, PSP Area,


Sector – 22, Rohini, New Delhi - 110085
MAHARAJA AGRASEN INSTITUTE OF TECHNOLOGY
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

VISION
"To attain global excellence through education, innovation, research, and work ethics in the
field of Computer Science and engineering with the commitment to serve humanity."

MISSION

M1: To lead in the advancement of computer science and engineering through internationally
recognized research and education.
M2: To prepare students for full and ethical participation in a diverse society and encourage
lifelong learning.
M3: To foster development of problem solving and communication skills as an integral
component of the profession.
M4: To impart knowledge, skills and cultivate an environment supporting incubation, product
development, technology transfer, capacity building and entrepreneurship in the field of
computer science and engineering.
M5: To encourage faculty, student‘s networking with alumni, industry, institutions, and other
stakeholders for collective engagement.
Rubrics for Lab Assessment:
10 Marks POs and PSOs Covered
Rubrics
0 Marks 1 Marks 2 Marks PO PSO

Is able to identify and define


PSO1,
R1 the objective of the given No Partially Completely PO1, PO2
PSO2
problem?

Is proposed
PO1,PO2, PSO1,
R2 design/procedure/algorithm No Partially Completely
PO3 PSO2
solves the problem?

Has the understanding of the


tool/programming language PO1,PO3, PSO1,
R3 No Partially Completely
to implement the proposed PO5 PSO2
solution?

Are the result(s) verified


PO2,PO4,
R4 using sufficient test data to No Partially Completely PSO2
PO5
support the conclusions?

PSO1,
R5 Individuality of submission? No Partially Completely PO8, PO12
PSO3
INDEX
R1 R2 R3 R4 R5
Has the
understa
Is able Is nding of Are the
to propose the result(s)
identify d design tool/pro verified
and /proced grammi using
Individu
define ure ng sufficie
ality of
Date Of the /algorith languag nt test Total Faculty
[Link] Experiment objectiv m e to data to
submiss
Performance ion? Marks Signature
e of the solves implem support
given the ent the the
problem problem propose conclusi
? ? d ons?
solution
?

2 2 2 2 2
Marks Marks Marks Marks Marks

Describing data, viewing and


1
manipulating data using Python.

To plot the probability


2
distribution curve.

To perform chi-square test on


3
various data sets.

To use Python as a
4 programming tool for the
analysis of data structures.

To perform various operations


5 such as data storage, analysis
and visualization.

To perform descriptive statistics


6
analysis and data visualization.

Toperform Principal Component


7
Analysis on datasets.

To perform linear regression on


8
datasets.

To perform Data Aggregation


9
and Group-Wise Operations.
EXPERIMENT NO. 1

AIM: Describing data, viewing and manipulating data using Python.

THEORY:
In data analysis, describing, viewing, and manipulating data are fundamental steps to
understand datasets and prepare them for further processing. Python provides powerful
libraries such as ‗NumPy‘ and ‗Pandas‘ that make these tasks efficient and user-friendly.
 Describing Data: Functions such as .describe(), .mean(), .median(), and .std() provide
statistical summaries (average, spread, min, max, quartiles).
 Viewing Data: Commands like .head(), .tail(), .info(), and .shape() help inspect
structure, size, and sample records of the dataset.
 Manipulating Data: Using indexing (loc, iloc), filtering, sorting, handling missing
values (dropna(), fillna()), grouping, merging, and reshaping, we can clean and
prepare data for analysis.
Describing data gives insights, viewing helps understanding, and manipulation ensures data is
ready for meaningful analysis.

CODE:

# --- STEP 1: Create a simple DataFrame ---


import pandas as pd
df = pd.read_csv("/content/[Link]")
pd.set_option('[Link]', 1000)

print("=== STEP 1: Dataset Loaded ===")


print([Link]())

# --- STEP 2: Describe the data ---


print("\n=== STEP 2: Data Description ===")
print([Link](include='all'))
print(―\n‖)
[Link]()
# --- STEP 3: View the data ---
# a) View specific rows (first 5 rows)
print("\n=== STEP 3a: Viewing Specific Rows ===")
print([Link][0:5])

# b) View specific columns (first 2 columns)


print("\n=== STEP 3b: Viewing Specific Columns ===")
print(df[[Link][:2]].head())
print("\n")
print([Link]())

# c) View rows on specific condition (e.g., 2nd column > mean)


print("\n=== STEP 3c: Rows with 2nd column > mean ===")
second_col = [Link][1]
if [Link].is_numeric_dtype(df[second_col]):
print(df[df[second_col] > df[second_col].mean()].head())
else:
print(f"Column '{second_col}' is not numeric, skipping condition filter.")

# --- STEP 4: Manipulate the data ---


# a) Add a new column based on conditions (e.g., High/Low based on 1st numeric col)
print("\n=== STEP 4a: Adding New Column Based on Condition ===")
first_num_col = df.select_dtypes(include='number').columns[0]
df["Category"] = ["High" if val > df[first_num_col].mean() else "Low" for val in
df[first_num_col]]
print(df[["Category"]].head())

# b) Sort the data in descending order by first numeric column


print("\n=== STEP 4b: Sorting Data in Descending Order ===")
print(df.sort_values(by=first_num_col, ascending=False).head())
# c) Grouping the data into specific groups (using new Category column)
print("\n=== STEP 4c: Grouping Data by Category ===")
print([Link]("Category")[first_num_col].mean())

# d) Removing rows with some specific condition (e.g., where first numeric column < mean)
print("\n=== STEP 4d: Removing Rows with Specific Condition ===")
filtered_df = df[df[first_num_col] >= df[first_num_col].mean()]
print(filtered_df.head())

OUTPUT:
Viva Questions & Answers
 Q: What does the describe() method return and why is it useful? A: Returns summary
statistics (count, mean, std, min, quartiles, max) for numerical columns. Useful for quick
statistical overview without detailed analysis, identifying outliers and data ranges .

 Q: How does Boolean indexing work in Pandas? A: Boolean indexing uses conditional
statements to create boolean masks (True/False arrays). These masks select DataFrame rows
where condition is True: df[df['column'] > value] .

 Q: Difference between .head() and .tail() methods? A: head() shows first n rows for
initial data overview: tail() shows last n rows for examining end of data. Both are non-
destructive and don't modify original data .

 Q: How do you select multiple columns from a DataFrame? A: Use double bracket
notation with list of column names: df[['col1', 'col2', 'col3']]. Single brackets
return Series for one column; double brackets return DataFrame for multiple columns .
EXPERIMENT NO. 2

AIM: To plot the probability distribution curve.

THEORY:
Probability distribution curves visually represent the likelihood of different outcomes in a
dataset. They are fundamental in statistical analysis for understanding data behaviour. A
probability distribution curve is a graphical representation that shows how the values of a
dataset are distributed. It helps us understand the likelihood of different outcomes occurring
within a dataset.
 It can be discrete (showing probabilities of distinct values, e.g., dice rolls) or
continuous (showing the density of values within a range, e.g., heights of students).
 In Python, probability distribution curves can be plotted using histograms with density
normalization and smooth probability density functions (PDFs).
 The x-axis represents the values of the variable, and the y-axis represents the
probability density.
CODE:
# --- STEP 1: Create a simple DataFrame ---
import pandas as pd
import [Link] as plt
df = pd.read_csv("/content/[Link]")
pd.set_option('[Link]', 1000)

# --- STEP 2: Display starting rows of the dataset ---


print("=== STEP 2: Starting Rows ===")
print([Link]())

# --- STEP 3: Display column names of the dataset ---


print("\n=== STEP 3: Column Names ===")
print([Link]())

# --- STEP 4: Specify columns to plot (choose numeric columns) ---


numeric_cols = df.select_dtypes(include=[[Link]]).columns
print("\n=== STEP 4: Numeric Columns Selected for Plotting ===")
print(numeric_cols)

# --- STEP 5: Plot probability distribution curve ---


[Link](figsize=(10,6))
for col in ['Feature7', 'Feature8']:
data = df[col].dropna()
[Link](data, bins=30, density=True, alpha=0.4, label=col)
# Smooth curve
counts, bin_edges = [Link](data, bins=30, density=True)
x_vals = (bin_edges[1:] + bin_edges[:-1]) / 2
[Link](x_vals, counts, linewidth=2)

# --- STEP 6: Add title and labels ---


[Link]("Probability Distribution Curves")
[Link]("Values")
[Link]("Probability Density")
[Link]()

OUTPUT:
EXPERIMENT NO. 3

AIM: To perform chi-square test on a dataset.

THEORY:
The Chi-Square (χ²) Test is a statistical method used to determine whether there is a
significant relationship between two categorical variables in a dataset. It compares the
observed frequencies in each category with the expected frequencies if there were no
association between the variables.
 Types of Chi-Square Tests:
1. Chi-Square Test of Independence → checks if two categorical variables are
related.
2. Chi-Square Goodness of Fit Test → checks if a sample follows a specific
distribution.
 Chi-Square Statistic Formula:

where:
 Oi= Observed frequency
 Ei = Expected frequency
 Decision Making:
o Null Hypothesis (H0): No association between the variables (they are
independent).
o Alternative Hypothesis (H1): There is an association between the variables.
o If p-value < α (0.05) → Reject H0, variables are significantly associated.
o If p-value ≥ α (0.05) → Fail to reject H0, no significant association.
The Chi-Square Test is widely used in research, biology, and social sciences to test
categorical data relationships.

CODE:
# --- STEP 1: Import the required libraries ---
import pandas as pd
import [Link] as stats
# --- STEP 2: Import the required Dataset ---
df = pd.read_csv("/content/children [Link]")
print("=== Dataset Sample ===")
print([Link]())

# --- STEP 3: Create a contingency table ---


contingency_table = [Link](df['Age in 5-year groups'], df['Anemia level'])
print("\n=== STEP 3: Contingency Table ===")
print(contingency_table)

# --- STEP 4: Perform the chi-square test ---


chi2, p, dof, expected = stats.chi2_contingency(contingency_table)

# --- STEP 5: Display the results ---


print("\n=== STEP 5: Chi-Square Test Results ===")
print(f"a) Chi-Square Statistic: {chi2}")
print(f"b) P-Value: {p}")
print(f"c) Degrees of Freedom: {dof}")
print(f"d) Expected Frequencies:\n{expected}")

# --- STEP 6: Determine if the result is significant ---


alpha = 0.05
print("\n=== STEP 6: Significance Check ===")
if p < alpha:
print(f"Since p-value ({p:.4f}) < {alpha}, we reject the null hypothesis → Significant
association exists.")
else:
print(f"Since p-value ({p:.4f}) >= {alpha}, we fail to reject the null hypothesis → No
significant association.")
OUTPUT:

Viva Questions & Answers


 Q: Explain the Chi-square test formula and what each component represents.

A: Formula: $\chi^{2}=\sum\frac{(O_{i}-E_{i})^{2}}{E_{i}}$. Numerator is squared


difference between observed and expected; denominator normalizes by expected frequency.
Larger $\chi^{2}$ means larger deviations 8.

 Q: What is the relationship between Chi-square statistic and p-value?

A: Chi-square statistic is compared to chi-square distribution with df degrees of freedom.


Larger $\chi^{2}$ values correspond to smaller p-values. Small p-value indicates statistic is
unlikely under null hypothesis 9.

 Q: Difference between goodness of fit test and test of independence?

A: Goodness of fit compares observed distribution against theoretical model with one
categorical variable. Test of independence examines relationship between two categorical
variables in contingency table 10.

 Q: What does it mean to fail to reject the null hypothesis?

A: Failing to reject $H_{0}$ means insufficient evidence to conclude that distributions differ.
It doesn't prove $H_{0}$ is true, just that observed data is consistent with null hypothesis at
chosen significance level 11.
EXPERIMENT NO. 4

AIM: To use Python as a programming tool for the analysis of data structures.

THEORY:
Python is a powerful, high-level programming language that is widely used for data analysis
and manipulation due to its simplicity, flexibility, and extensive libraries. In the context of
data structures, Python provides built-in types such as lists, tuples, sets, and dictionaries, each
optimized for different kinds of data storage and operations.
 Lists → Ordered, mutable collections that allow indexing, slicing, and modifications.
 Tuples → Ordered, immutable collections, often used for fixed data.
 Sets → Unordered collections of unique elements, useful for mathematical operations
like union, intersection, and difference.
 Dictionaries → Key-value pair structures, allowing fast retrieval and updates.

CODE:
# --- STEP 1: Perform operations on List ---
print("=== STEP 1: LIST OPERATIONS ===")
# a) Access Elements
my_list = [10, 20, 30, 40, 50]
print("Original List:", my_list)
print("a) Access 1st element:", my_list[0])
print("a) Access last element:", my_list[-1])
# b) Perform Slicing
print("b) Slice first 3 elements:", my_list[:3])
print("b) Slice from 2nd to 4th element:", my_list[1:4])
# c) Modify Elements
my_list[2] = 100
print("c) After modifying 3rd element:", my_list)
# d) Append Elements
my_list.append(60)
print("d) After appending 60:", my_list)
# e) Remove Elements
my_list.remove(20)
print("e) After removing 20:", my_list)

# --- STEP 2: Perform operations on Dictionaries ---


print("\n=== STEP 2: DICTIONARY OPERATIONS ===")
# a) Create a dictionary
my_dict = {"name": "Alice", "age": 21, "city": "Delhi"}
print("Original Dictionary:", my_dict)
# b) Access values
print("b) Access 'name':", my_dict["name"])
# c) Modify values
my_dict["age"] = 22
print("c) After modifying 'age':", my_dict)
# d) Add new key-value pair
my_dict["college"] = "ABC University"
print("d) After adding new key-value pair:", my_dict)
# e) Remove existing key-value pair
my_dict.pop("city")
print("e) After removing 'city':", my_dict)

# --- STEP 3: Perform operations on Tuples ---


print("\n=== STEP 3: TUPLE OPERATIONS ===")
# a) Create a tuple
my_tuple = (1, 2, 3, 4, 5)
print("Original Tuple:", my_tuple)
# b) Access Elements
print("b) Access 1st element:", my_tuple[0])
print("b) Access last element:", my_tuple[-1])
# c) Perform Slicing
print("c) Slice first 3 elements:", my_tuple[:3])
print("c) Slice from 2nd to 4th element:", my_tuple[1:4])

# --- STEP 4: Perform operations on Sets ---


print("\n=== STEP 4: SET OPERATIONS ===")
# a) Create a Set
my_set = {10,10, 20, 30}
print("Original Set:", my_set)
# b) Insert Elements
my_set.add(40)
print("b) After adding 40:", my_set)
# c) Remove Elements
my_set.remove(20)
print("c) After removing 20:", my_set)

OUTPUT:
EXPERIMENT NO. 5

AIM: To perform various operations such as data storage, analysis and visualization.

THEORY:
In the modern era of computing, handling and interpreting data is a fundamental task. Python
provides powerful tools and libraries that allow users to efficiently perform data storage,
analysis, and visualization in a simple and effective manner.
 Data Storage: Data can be stored in Python using built-in data structures like lists,
tuples, dictionaries, and sets. For larger datasets, specialized libraries such as Pandas
provide structures like DataFrame and Series, which allow handling tabular data
similar to spreadsheets or databases.
 Data Analysis: Using libraries like NumPy and Pandas, Python enables fast and
efficient operations such as filtering, grouping, aggregation, descriptive statistics, and
correlation analysis. These operations help in discovering patterns, relationships, and
insights from raw data.
 Data Visualization: Visualization converts complex numerical data into graphical
forms, making patterns and trends easier to understand. Libraries like Matplotlib and
Seaborn are widely used to create charts such as histograms, scatter plots, boxplots,
and distribution curves.
By combining these three aspects, Python serves as a complete tool for working with data—
from storage to analysis and finally visualization, making it a cornerstone of modern data
science and analytics.

CODE:
# --- STEP 1: Import the required libraries ---
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import fetch_california_housing
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from [Link] import mean_squared_error, r2_score
# --- STEP 2: Import the required Dataset ---
housing = fetch_california_housing(as_frame=True)
df = [Link]
print("Dataset Preview:")
print([Link]())
print("\nSummary Statistics:")
print([Link]())

# --- STEP 3: Linear Regression over the data ---


X = df[['MedInc']]
y = df['MedHouseVal']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("\nLinear Regression Results:")
print("Intercept:", model.intercept_)
print("Coefficient:", model.coef_[0])
print("R² Score:", r2_score(y_test, y_pred))
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))

# --- STEP 4: Data Manipulation ---


print("\nData Manipulation Examples:")
# Create a new column: price per room
df['PricePerRoom'] = df['MedHouseVal'] / df['AveRooms']
print(df[['MedHouseVal', 'AveRooms', 'PricePerRoom']].head())
# Filtering: Houses with Median Income > 8
high_income = df[df['MedInc'] > 8]
print("\nHigh Income Areas:")
print(high_income.head())
# --- STEP 5: Visualizing Data ---
[Link](figsize=(14,4))
[Link](1,2,1)
[Link](x='MedInc', y='MedHouseVal', data=df, alpha=0.5)
[Link](X_test, y_pred, color='red') # regression line
[Link]("Median Income vs House Value")
[Link](1,2,2)
[Link](df['MedHouseVal'], bins=30, kde=True, color="green")
[Link]("Distribution of House Values")
[Link]()

OUTPUT:
EXPERIMENT NO. 6

AIM: To perform descriptive statistics analysis and data visualization.

THEORY:
Descriptive statistics summarize and describe the main features of a dataset through
numerical and graphical methods.
Python‘s Pandas library provides the .describe() function to compute key statistics like mean,
median, standard deviation, and quartiles. These measures help in understanding data
distribution and variability.
Further, data visualization techniques like histograms, boxplots, and scatter plots are used to
observe data patterns, detect outliers, and understand relationships among variables.
Histograms show frequency distributions, boxplots display spread and skewness, and scatter
plots illustrate relationships between two continuous variables. Together, these techniques
form the foundation of exploratory data analysis (EDA), which is crucial before applying any
modeling techniques.
Performing descriptive statistics and data visualization in Python typically involves the
following steps:
1. Load Data
2. Calculate Descriptive Statistics
3. Visualize the Data
We'll use popular libraries such as pandas, numpy, matplotlib, and seaborn to accomplish
these tasks.

CODE:
# Step 1: Import required libraries
import pandas as pd
import [Link] as plt
import seaborn as sns

# Step 2: Import dataset


df = sns.load_dataset('tips')
print("Sample Data:\n", [Link]())
# Step 3: Perform descriptive statistics
print("\nDescriptive Statistics:")
print([Link](include='all'))

# Step 4: Group descriptive statistics by 'sex'


group_stats = [Link]('sex').describe()
print("\nGrouped Descriptive Statistics by Sex:")
print(group_stats)

# Step 5: Generate histogram, boxplot, scatterplot


[Link](figsize=(15,5))
[Link](1,3,1)
[Link](df['total_bill'], bins=20, color='skyblue', edgecolor='black')
[Link]("Histogram of Total Bill")
[Link]("Total Bill")
[Link]("Frequency")

[Link](1,3,2)
[Link](x='day', y='total_bill', data=df, palette='Set2')
[Link]("Boxplot of Total Bill by Day")
[Link]("Day")
[Link]("Total Bill")

[Link](1,3,3)
[Link](df['total_bill'], df['tip'], alpha=0.7, edgecolor='k')
[Link]("Scatter Plot: Total Bill vs Tip")
[Link]("Total Bill")
[Link]("Tip")
[Link]()
OUTPUT:
EXPERIMENT NO. 7

AIM: To perform Principal Component Analysis on datasets.

THEORY:
Principal Component Analysis (PCA) is a dimensionality reduction technique that transforms
high-dimensional data into fewer uncorrelated variables called principal components.
These components capture the maximum variance in the data.
PCA is essential when dealing with large datasets where features may be correlated.
It helps in simplifying models, visualizing multidimensional data, and improving
computational efficiency.
The process involves standardizing data, computing covariance matrix, extracting
eigenvectors/eigenvalues, and projecting data onto new axes representing principal
components. Python‘s [Link] makes this process simple and efficient.

CODE:
# Step 1: Import libraries
import pandas as pd
from [Link] import StandardScaler
from [Link] import PCA
import [Link] as plt
import seaborn as sns

# Step 2: Import dataset


df = pd.read_csv('/content/[Link]')
print([Link]())

# Step 3: Select columns for PCA (e.g., numeric socio-economic indicators)


features = ['child_mort', 'exports', 'health', 'imports', 'income', 'inflation', 'life_expec',
'total_fer', 'gdpp']
x = df[features].dropna()
# Step 4: Check if valid (all numeric, no missing)
print([Link]()); print([Link]())

# Step 5: Standardize data


scaler = StandardScaler()
x_scaled = scaler.fit_transform(x)

# Step 6: Apply PCA


pca = PCA(n_components=2)
pca_result = pca.fit_transform(x_scaled)
print("PCA-components shape:", pca_result.shape)
print("Explained variance ratio:", pca.explained_variance_ratio_)

# Step 7: Plot PCA results


[Link](figsize=(8,6))
[Link](x=pca_result[:,0], y=pca_result[:,1], hue=df['country'], palette='Set1',
alpha=0.7)
[Link]("PCA of Country Data")
[Link]("Principal Component 1")
[Link]("Principal Component 2")
[Link](title='Country')
[Link]()

OUTPUT:
EXPERIMENT NO. 8

AIM: To perform linear regression on datasets.

THEORY:
Linear Regression is a machine learning algorithm used to model the relationship between
one or more independent variables (features) and a dependent variable (target).
It assumes a linear relationship between them, expressed as Y = aX + b, where a is the slope
and b is the intercept.
The goal is to minimize the difference between predicted and actual values using the least
squares method.
In Python, the LinearRegression class from sklearn.linear_model is commonly used.
Performance is evaluated using metrics like Mean Squared Error (MSE) and R² Score.
Visualization of regression lines helps in understanding the fit of the model.

CODE:
# Step 1: Import libraries
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score

# Step 2: Import dataset


df = pd.read_csv('/content/Salary_dataset.csv')
print([Link]())

# Step 3: Define features and target


features = ['YearsExperience']
target = 'Salary'
# Step 4: Prepare matrix & vector
X = df[features].[Link](-1,1)
y = df[target].values

# Step 5: Split data


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 6: Create & fit the model


model = LinearRegression()
[Link](X_train, y_train)

# Step 7: Make predictions


y_pred = [Link](X_test)

# Step 8: Calculate performance metrics


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse:.2f}")
print(f"R^2 Score: {r2:.2f}")

# Step 9: Visualize results


[Link](figsize=(8,6))
[Link](X_train, y_train, color='blue', label='Training data')
[Link](X_test, y_test, color='green', label='Test data')
[Link](X_test, y_pred, color='red', label='Regression line')
[Link]("Linear Regression: Salary vs Years of Experience")
[Link]("Years of Experience")
[Link]("Salary")
[Link]()
[Link]()

OUTPUT:
EXPERIMENT NO. 9

AIM: To perform Data Aggregation and Group-wise Operations.

THEORY:
Data aggregation is the process of summarizing and combining data for analysis.
It allows the computation of statistical measures like mean, sum, max, min, and range across
specific groups.
Using groupby() and agg() in Pandas, data can be segmented into groups based on one or
more columns and aggregated using functions.
This helps uncover patterns across categories and simplifies large datasets.
Pivot tables further allow multi-dimensional summarization, providing insights from various
perspectives.

CODE:
# Step 1: Import libraries
import pandas as pd

# Step 2: Import dataset


df = pd.read_csv('/content/[Link]')
print([Link]())

# Step 3: Group some specific columns


group_column = 'Pclass' # for example passenger class
agg_columns = ['Fare', 'Age']

# Step 4: : Calculate the mean and sum


grouped_df = [Link](group_column)[agg_columns].agg(['mean', 'sum'])
print("\nAggregated Data (mean & sum):")
print(grouped_df)
# Step 5: Custom aggregate: max, min, range
def range_func(series):
return [Link]() - [Link]()

custom_agg = [Link](group_column)['Fare'].agg(['max','min', range_func])


print("\nCustom Aggregated Data (Fare by Pclass):")
print(custom_agg)

# Step 6: Multiple aggregation functions on different columns


multi_agg = [Link]('Sex').agg({'Fare':['mean','max'], 'Age':['min','median']})
print("\nMultiple Aggregations (by Sex):")
print(multi_agg)

# Step 7: Transform: create new column and apply transform


df['Fare_per_person'] = df['Fare'] / (df['SibSp']+df['Parch']+1)
df['MeanFareByClass'] = [Link]('Pclass')['Fare_per_person'].transform('mean')
print(df[['Pclass','Fare_per_person','MeanFareByClass']].head())

# Step 8: Create Pivot table for multi-dimensional aggregation


pivot_table_df = pd.pivot_table(df, values='Fare', index='Pclass', columns='Sex',
aggfunc='mean')
print("\nPivot Table (Mean Fare by Pclass & Sex):")
print(pivot_table_df)

OUTPUT:
Viva Questions & Answers
 Q: How do you perform multiple aggregations on different columns? A: Use .agg()
with column-function mapping: [Link]('group').agg({'col1': 'mean', 'col2':
'sum'}) or use .agg(dict(col1='mean', col2='sum')) .

 Q: What happens if a groupby column has missing values? A: By default, groupby()


excludes rows with missing values in grouping column. Use dropna=False parameter to
include NaN as separate group. This ensures all data considered .

 Q: Can you group by calculated columns or functions of columns? A: Yes, using


groupby(df['col'].apply(function)) or creating temporary column. For example,
groupby month from datetime: [Link](df['date'].[Link])) or use [Link]() for
binned grouping .

You might also like