Create beginner Works with: Any AI

Groupby and Aggregation Recipe Builder

Structured brief

Choose the right aggregation for a business question.

The Open buttons send the prompt text as currently shown below to that site only when you click. Fill in the placeholders first.

Rate this prompt

Loading ratings…

Turn my business questions into pandas groupby code I understand. Columns and types: [COLUMNS_AND_TYPES]. A few sample rows: [SAMPLE_ROWS]. Questions I want answered, for example revenue by month and region, top 5 customers, share of orders returned: [QUESTIONS].

For each question deliver:
- Restated question in one precise sentence (unit, period, filter).
- The grain of the result (one row per what).
- The code, using named aggregation (df.groupby(keys).agg(total=("amount","sum"))) and sort_values.
- A tiny worked example: 6 invented rows with the exact output the code would give, so I can verify my understanding.
- One trap for that question (for example averaging averages, counting nulls, filtering before versus after grouping, groupby dropping null keys unless dropna=False).
- A check I can run to confirm the totals still match the source.

Then show the difference between transform, agg, and apply with one example each on my data, and explain which to choose when I need a group total attached to every row. Finish with a short list of questions where the data cannot give the answer, and why.

Fill in before use

Optional: type your details and the prompt above updates. Nothing is saved or sent.

Replace each bracketed placeholder with your own details, then paste into Any AI or another assistant.

Related prompts