Pandas DataFrame Exercise 1: Basic Data Handling & Queries

Build muscle memory with core Pandas methods. In this hands-on exercise, load the benchmark 35-row student dataset (student.xlsx), inspect dimensions and data types, slice records, filter by conditional thresholds, calculate summary statistics, and query unique categorical values.

Dataset Overview & Excel Download 🔝

The 35-row student benchmark dataset contains five columns: id, name, class, mark, and gender. You can download the workbook or copy the dictionary generator from the Sample Student DataFrame guide:

Download student.xlsx (35 Rows) ↓ View Dataset Guide →

Video Guide: Solving DataFrame Basic Questions 🔝

Practice Problems Overview (Q1 to Q10) 🔝

  1. Read the Excel file download/student.xlsx into a DataFrame using id as the index.
  2. Find the dimensions (shape) and total row count.
  3. Display the first 4 rows using head().
  4. Display the list of column names and their data types.
  5. Extract the first 5 records of the name column.
  6. Display the top 5 highest-scoring students based on the mark column using sort_values().
  7. List all unique classes in the class column and count total unique classes (nunique()).
  8. Filter and display students where mark > 60.
  9. Filter students enrolled in class 'Three' with marks greater than 50 using combined boolean criteria.
  10. Compute the average, maximum, and total sum of marks.

Step-by-Step Code Solutions & Assertions 🔝

import pandas as pd

# 1. Read Excel file
df = pd.read_excel("download/student.xlsx", index_col="id")

# 2. Dimensions & Row Count
print("Shape:", df.shape)  # (35, 4)
print("Total Rows:", len(df))
assert df.shape[0] == 35
assert len(df) == 35

# 3. First 4 Rows
print("
First 4 Rows:
", df.head(4))

# 4. Column Names
print("
Columns:", df.columns.tolist())

# 5. First 5 Names
print("
First 5 Names:
", df["name"].head(5))

# 6. Top 5 Marks
top5_marks = df.sort_values(by="mark", ascending=False).head(5)
print("
Top 5 Highest Marks:
", top5_marks)
assert top5_marks.iloc[0]["name"] == "Kenn Rein"
assert top5_marks.iloc[0]["mark"] == 96

# 7. Unique Classes
classes = df["class"].unique()
print("
Unique Classes:", classes)
print("Class Count:", df["class"].nunique())
assert df["class"].nunique() == 7

# 8. Mark > 60 Filter
high_scorers = df[df["mark"] > 60]
print(f"
Students with Mark > 60: {len(high_scorers)}")
assert len(high_scorers) > 0

# 9. Multiple Conditions: Class Three and Mark > 50
class3_passed = df[(df["class"] == "Three") & (df["mark"] > 50)]
print(f"Class Three with Mark > 50: {len(class3_passed)}")

# 10. Summary Metrics
print(f"Average Mark: {df['mark'].mean():.2f}")
print(f"Highest Mark: {df['mark'].max()}")
print(f"Total Mark Sum: {df['mark'].sum()}")
assert df["mark"].max() == 96

Exercise Series Navigation 🔝

  • Exercise 1 (Current Page): Basic data queries, marks, and unique values.
  • Exercise 1-1: Data binning with cut(), groupby distributions, and charts.
  • Exercise 2: Search Console query analytics, string length, and click analysis.
  • Exercise 3 Series: Time-series maintenance logs and datetime queries.

Practice in Google Colab & GitHub 🔝

Open in Google Colab View on GitHub
Run all 10 student dataset query solutions interactively in Google Colab. The 35-row benchmark dataset is directly embedded.




Subscribe to our YouTube Channel here



plus2net.com



14-07-2021

You are doing amazing job.




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer