Pandas → T-SQL › groupby

Pandas groupby to SQL GROUP BY

Every column you keep that is not aggregated goes in the GROUP BY. SQL Server will shout if you forget.

SELECT customer_id,
       SUM(total_amount) AS sales,
       COUNT(order_id) AS n
FROM Orders
GROUP BY customer_id;

as_index=False is just “customer_id stays a column,” which it already is in SQL.

Canonical translation

SELECT customer_id,
       SUM(total_amount) AS sales,
       COUNT(order_id) AS n
FROM Orders
GROUP BY customer_id;

Then write it on a ticket

Monday is free. A colleague asks, you type, we mark it.

Clock in free