Chapter 19: Pandas Quiz
Pandas Quiz — designed like we are in a classroom or a 1-on-1 mentoring session.
The goal is not to get everything right on the first try. The goal is to see what you already understand well, what feels a bit shaky, and what we should practice more together.
I prepared three levels:
- Level 1 — fundamental concepts almost everyone needs to be comfortable with
- Level 2 — typical day-to-day tasks in real projects
- Level 3 — slightly trickier / more realistic situations
For each question you get:
- clear task
- small ready-to-use dataset
- space to think / write your answer
- after you try → detailed solution + explanation + common mistakes people make
Instructions
- Copy the dataset into your notebook / online editor
- Try to write the code yourself first — even if it’s not perfect
- Only look at the solution after you have attempted it
- After finishing some questions, come back and tell me:
- which questions felt easy
- which ones were difficult / surprising
- where you got stuck or made mistakes
Let’s begin!
Level 1 – Core foundations
Q1. Selection + filtering + sorting Select only people older than 25 who live in Pune or Mumbai. Show only columns: name, city, salary Sort them by salary descending.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
import pandas as pd df = pd.DataFrame({ 'name': ['Priya', 'Rahul', 'Ananya', 'Sneha', 'Vikram', 'Meera', 'Arjun', 'Neha'], 'age': [24, 31, 19, 28, 45, 22, 33, 27], 'city': ['Pune', 'Hyderabad', 'Bangalore', 'Mumbai', 'Pune', 'Chennai', 'Mumbai', 'Pune'], 'salary': [72000, 145000, 88000, 112000, 210000, 68000, 95000, 105000] }) |
Q2. Creating a new column with condition Create column bonus_pct:
- 12% if salary > 100_000
- 8% otherwise
Then create bonus_amount = salary × bonus_pct Show both new columns rounded to nearest whole number.
Q3. Simple aggregation question What is the average salary? How many people earn more than this average? Show their names and salaries.
Q4. Value counts with percentage Show how many people are in each city — both as count and as percentage (1 decimal place).
Level 2 – Everyday realistic tasks
Q5. Groupby + multiple aggregations Group by city and show:
- number of employees (count)
- average salary
- highest salary
- percentage of people earning > 100_000
Round numbers appropriately.
Q6. Rank within group Create column rank_in_city — salary rank within each city (1 = highest salary in that city)
Show name, city, salary, rank_in_city Sort by city and then by rank.
Q7. Categorizing with conditions Create column level:
- “Senior” if salary ≥ 120_000
- “Mid” if salary ≥ 90_000
- “Junior” otherwise
Show how many people in each level (value_counts)
Q8. Top N per group Show the top 2 highest earners in each city Include: name, city, salary (If a city has fewer than 2 people — show all)
Level 3 – Slightly more realistic / tricky
Q9. Filling missing values with group median Make two salaries missing:
|
0 1 2 3 4 5 6 7 |
df.loc[2, 'salary'] = np.nan df.loc[5, 'salary'] = np.nan |
Fill missing salaries with the median salary of their own city. If the city has no valid salaries → use overall median.
Q10. Conditional filtering with group stats Show people who earn more than both:
- their city’s average salary
- the overall company average
Show: name, city, salary, city_avg, overall_avg
Q11. Nice summary table Create a table like this (you can use groupby + agg + round):
|
0 1 2 3 4 5 6 7 8 9 10 |
City | Count | Avg Salary | % >100k | Highest Earner | Their Salary ----------|-------|------------|---------|----------------|------------- Pune | ... | ... | ... | ... | ... Mumbai | ... | ... | ... | ... | ... ... |
Q12. Merge + filter You receive this additional table:
|
0 1 2 3 4 5 6 7 8 9 10 |
managers = pd.DataFrame({ 'city': ['Pune', 'Mumbai', 'Hyderabad', 'Bangalore', 'Chennai'], 'manager': ['Neha', 'Rohan', 'Suresh', 'Kavita', 'Amit'], 'manager_since_year': [2020, 2019, 2022, 2021, 2023] }) |
Left-join it to the main DataFrame on city. Then show only employees whose manager has been in position for less than 3 years (as of 2025).
How to make the most of this quiz
- Do not look at solutions until you have tried
- Write code even if you are not 100% sure — guessing is learning
- After 4–6 questions, come back and tell me:
- which ones were comfortable
- which ones felt difficult / confusing
- any error messages you got
- what surprised you
Then we can:
- explain the tricky ones in more depth
- give follow-up exercises on weak spots
- move to next topic with stronger foundation
Which questions do you want to start with? Or would you like me to give solutions one by one after you try?
You can also say:
- “I tried Q1–Q4, here is what I got…”
- “Q9 looks hard — can you explain first?”
- “Give me solutions for level 1 only”
Just tell me how you want to proceed — we’ll go at your pace. 😊
