Home › Interview Questions › Write a pandas pipeline that removes duplicate ord…

Write a pandas pipeline that removes duplicate orders by 'order_id', filters for status 'Completed', and then groups by 'customer_id' to compute the total amount and count of orders.

🟡 Medium Coding Junior level
1Times asked
Nov 2026Last seen
Nov 2026First seen

💡 Model Answer

You can chain pandas operations to clean and aggregate the data:

python
import pandas as pd

df = pd.DataFrame([...])  # your data
# 1. Remove duplicate orders
clean = df.drop_duplicates(subset='order_id')
# 2. Keep only completed orders
clean = clean[clean['status'] == 'Completed']
# 3. Group by customer and aggregate
result = clean.groupby('customer_id').agg(
    total=('amount', 'sum'),
    count=('order_id', 'count')
).reset_index()
print(result)

This runs in O(n) time and uses O(k) additional memory, where n is the number of rows and k is the number of unique customers.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500