TGViewer
Python for Data Analysts Python for Data Analysts @pythonanalyst · 51.8K subscribers
Post #1333 5.57K
🔥 Pandas Scenario-Based Interview Question 🐼

📊 Scenario:

You have an orders dataset with:
order_id
customer_id
order_date
category
sales

🎯 Task:

Find the top-selling category for each month based on total sales.

✅ Pandas Solution:

import pandas as pd

# Convert to datetime
df['order_date'] = pd.to_datetime(df['order_date'])

# Extract month
df['month'] = df['order_date'].dt.strftime('%b-%Y')

# Total sales by month & category
sales_summary = (
df.groupby(['month', 'category'])['sales']
.sum()
.reset_index()
)

# Rank categories within each month
sales_summary['rank'] = (
sales_summary.groupby('month')['sales']
.rank(method='dense', ascending=False)
)

# Top category per month
result = sales_summary[sales_summary['rank'] == 1]

print(result)

💡 Concepts Tested:

✔️ groupby()
✔️ Date handling
✔️ Aggregation
✔️ Ranking within groups

React ♥️ for more interview questions
  • ❤ 11
More from @pythonanalyst
  1. Oct 2, 2026Skills for data analyst
  2. Sep 24, 2026GigaChat 3.5 Reasoning is a new open-source LLM designed to reason before generating respo…
  3. Sep 23, 2026Understanding Popular ML Algorithms: 1️⃣ Linear Regression: Think of it as drawing a strai…
  4. Aug 22, 2026How to Become a Data Analyst from Scratch! 🚀 Whether you're starting fresh or upskilling,…
  5. Aug 20, 2026🚀 Top Python Careers You Should Know 🐍 Python Developer 🤖 AI / Machine Learning Enginee…
  6. Aug 13, 2026📊 Python for Data Science – Complete Beginner Roadmap 🐍🚀 🔹 What is Data Science? Data…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →