Two centers in Dehradun — Kanwali Road & Premnagar

Pivot Edu Unit Practice & Tutorials

Q1: Load and Explore a CSV Dataset Using Pandas

Write a Python program to load a CSV file named students.csv and display the first five records, number of rows and columns, column names, data types, and basic statistical information. Logic: import Pandas, read the CSV file using read_csv(), and use head(), shape, columns, dtypes, and describe() to understand the dataset.

import pandas as pd

# Load the dataset
df = pd.read_csv("students.csv")

# Display first 5 rows
print("First 5 Rows:")
print(df.head())

# Display number of rows and columns
print("\nDataset Shape:")
print(df.shape)

# Display column names
print("\nColumn Names:")
print(df.columns)

# Display data types
print("\nData Types:")
print(df.dtypes)

# Display statistical summary
print("\nStatistical Summary:")
print(df.describe())

Q2: Handle Missing Values in a Dataset

Given a dataset named employees.csv containing columns such as Name, Age, Salary, and Department, identify missing values and replace missing numerical values with the column mean. Logic: load the dataset, check missing values with isnull().sum(), calculate the mean, and use fillna() to replace missing values.

import pandas as pd

# Load dataset
df = pd.read_csv("employees.csv")

# Check missing values
print("Missing Values Before Cleaning:")
print(df.isnull().sum())

# Fill missing Age values with mean age
df["Age"] = df["Age"].fillna(df["Age"].mean())

# Fill missing Salary values with mean salary
df["Salary"] = df["Salary"].fillna(df["Salary"].mean())

print("\nMissing Values After Cleaning:")
print(df.isnull().sum())

print("\nCleaned Dataset:")
print(df)

Q3: Perform Data Analysis Using GroupBy

Consider a sales dataset containing Product, Category, Quantity, and Sales columns. Find the total sales for each product category and identify the category with the highest total sales. Logic: group the dataset by Category, calculate the sum of Sales, sort the result, and select the category with the maximum value.

import pandas as pd

# Load sales dataset
df = pd.read_csv("sales.csv")

# Calculate total sales by category
total_sales = df.groupby("Category")["Sales"].sum()

print("Total Sales by Category:")
print(total_sales)

# Find category with highest sales
highest_category = total_sales.idxmax()
highest_sales = total_sales.max()

print("\nHighest Selling Category:")
print(highest_category)
print("Total Sales:", highest_sales)

Q4: Find Correlation Between Numerical Variables

Using a student dataset containing StudyHours, Attendance, and Marks columns, calculate the correlation between these numerical variables. Determine which variables have a strong positive relationship with Marks. Logic: select the numerical columns, calculate the correlation matrix using corr(), and inspect the correlation values.

import pandas as pd

# Load dataset
df = pd.read_csv("students.csv")

# Select numerical columns
columns = ["StudyHours", "Attendance", "Marks"]

# Calculate correlation matrix
correlation = df[columns].corr()

print("Correlation Matrix:")
print(correlation)

print("\nCorrelation with Marks:")
print(correlation["Marks"].sort_values(ascending=False))

Q5: Create a Data Visualization Using Matplotlib

Load a sales dataset and create a bar chart showing total sales for each product category. Logic: group sales by category, calculate total sales, and use Matplotlib to create a bar chart with suitable labels and a title.

import pandas as pd
import matplotlib.pyplot as plt

# Load dataset
df = pd.read_csv("sales.csv")

# Calculate total sales by category
total_sales = df.groupby("Category")["Sales"].sum()

# Create bar chart
total_sales.plot(kind="bar")

plt.title("Total Sales by Category")
plt.xlabel("Category")
plt.ylabel("Total Sales")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Q6: Build a Simple Linear Regression Model

Use a dataset containing StudyHours and Marks to predict a student's marks based on study hours. Split the data into training and testing sets, train a Linear Regression model, make predictions, and calculate the R² score. Logic: select StudyHours as the independent variable and Marks as the target variable, split the dataset, train LinearRegression, predict test values, and evaluate the model.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score

# Load dataset
df = pd.read_csv("students.csv")

# Independent and dependent variables
X = df[["StudyHours"]]
y = df["Marks"]

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

# Create and train model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate model
score = r2_score(y_test, y_pred)

print("Predicted Marks:")
print(y_pred)

print("\nR2 Score:", score)

Q7: Detect Outliers Using the IQR Method

Analyze a salary column and identify unusually high or low salary values using the Interquartile Range (IQR) method. Logic: calculate Q1 and Q3, find IQR, define lower and upper boundaries, and filter records outside those boundaries.

import pandas as pd

# Load dataset
df = pd.read_csv("employees.csv")

# Calculate Q1 and Q3
Q1 = df["Salary"].quantile(0.25)
Q3 = df["Salary"].quantile(0.75)

# Calculate IQR
IQR = Q3 - Q1

# Calculate boundaries
lower_limit = Q1 - 1.5 * IQR
upper_limit = Q3 + 1.5 * IQR

# Find outliers
outliers = df[
    (df["Salary"] < lower_limit) |
    (df["Salary"] > upper_limit)
]

print("Lower Limit:", lower_limit)
print("Upper Limit:", upper_limit)

print("\nOutlier Records:")
print(outliers)

Frequently Asked Questions