Data scientist
Senior
technical
A large e-commerce company wants to analyze customer behavior over time. You are tasked with aggregating transactions by customer ID to compute the total spent and number of purchases each month. Describe your approach using Pandas, including how you would handle date parsing and grouping by month.
def monthly_customer_summary(df):
df['order_date'] = pd.to_datetime(df['order_date'])
df['month'] = df['order_date'].dt.to_period('M')
summary = df.groupby(['customer_id', 'month']).agg(total_spent=('total_amount', 'sum'), purchase_count=('order_id', 'count'))
return summary.reset_index()
Trusted by 100+ professionals preparing for interviews
Trusted by 100+ professionals
50+ Company Question Banks
5+ Supported Languages
Practice More Questions Like This
Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.
Get Started Free