1. Lighthouse

Level 4 · Wide or long?

The request

From Priya Shah, Head of Data

“Before you build more reports: I keep seeing wide tables passed between scripts. Tell me which shape you'd store and work in, and when you'd switch.”

Your goal

Choose the best rule of thumb.

You have monthly revenue for three channels. Which statement about long and wide shapes is right?

Pick one answer, then check it.

The lesson

Two shapes, same numbers

Monthly revenue for three channels can be written two ways.

Long: one row per observation, with a column for each variable.

#   month    channel  revenue
#   2025-01  app       ...
#   2025-01  web       ...
#   2025-01  marketplace ...
#   2025-02  app       ...

Wide: one row per month, one column per channel.

#   month    app   marketplace   web
#   2025-01  ...   ...           ...
#   2025-02  ...   ...           ...

Nothing is lost either way. pivot_table turns long into wide, melt turns wide into long, and you can go back and forth as often as you like.

Why analysts work long

The long table is sometimes called tidy: each variable is a column, each observation is a row. That’s the shape pandas tools assume.

  • groupby("channel") needs channel to be a column of values. In wide form there’s nothing to group by; each channel is its own column.
  • A filter like df[df["channel"] == "web"] is one line. In wide form you’d select a column instead, and filters on several channels get awkward.
  • merge matches rows on key columns, such as month and channel. A budget sheet melted to long form joins straight onto actual spend.
  • Plotting libraries draw one line per group from a long table, or one line per column from a wide one. Both work, but the long table keeps working when a fourth channel appears.

That last point is the big one. When Harbor & Pine adds a channel, a long table simply gets more rows. A wide table gets a new column, and every line of code that named the columns one by one has to change.

When wide wins

People read grids. A manager scanning revenue for 36 months and three channels wants months down the side and channels across, not 108 rows. Wide is also the right shape for a correlation matrix or a heatmap, where rows and columns are both meaningful.

So the rule of thumb: store and compute in long form, and pivot to wide as the last step, for the reader.

Common mistakes

  • Keeping a spreadsheet’s wide layout in code and repeating each calculation per column.
  • Thinking the two shapes hold different information. They don’t; reshaping is lossless as long as each cell has one value.
  • Pivoting early, then trying to group or merge the grid.

Pick what may run on this site. You can change this any time from "Cookie settings" at the bottom of every page.