§2.3

Reshaping Data

The regional manager has a single spreadsheet on her desk. Across the top, three columns: sales_2024_01, sales_2024_02, sales_2024_03. Down the side, three rows, one per store. Nine numbers in a tidy grid that fits on a slide. When her analyst opens the same data in R to make a trend chart, the first thing he does is reshape it: one row per store-month, three columns instead of nine cells. The numbers are identical. The shape is not. And the shape determines which questions are easy to ask. The spreadsheet shape is friendly to the eye; the reshaped version is friendly to the chart engine. Reshaping is not a transformation — no information is added or lost. It is a rearrangement, and knowing when to rearrange is the difference between a chart that takes ten seconds to make and one that takes an hour.

The executive question: why does the same data need different shapes for spreadsheets, dashboards, and models?

Two shapes: wide and long

The same numbers live in two canonical shapes. Wide format puts one row per unit and spreads time, category, or measurement across the columns. Long format puts one row per unit-period (or unit-category, or unit-measurement) and uses a small fixed set of columns. Either shape can describe the same business reality. Neither is more correct than the other. They are useful for different things.

Wide is the natural shape for a human reading a spreadsheet. A regional director who opens last quarter's revenue file expects to see store names down the left, months across the top, and a number at each intersection. That arrangement compresses three pieces of information per cell — store, month, revenue — into a position-encoded grid that a reader can scan visually. Adding a fourth month means adding a column, which is fine for a small grid but a schema change for the underlying file: anything downstream that referenced sales_2024_03 will need to learn about sales_2024_04.

Long is the natural shape for almost everything that is not a human reading a spreadsheet. A line chart needs one column for time, one for the value, and (if there are multiple lines) one for the series. A regression needs one row per observation, with the predictors as fixed-name columns. A dashboard filter — "show me Store A, Q1 only" — needs the store and month to be values inside the table, not column headers. In all three cases, the wide grid has to be unpivoted first. The same nine numbers, the same business reality, two shapes, and the long shape is the one that connects to the rest of the analytical pipeline. Figure 1 shows the same Bean & Basket quarter both ways.

One row per store, one column per month. Reads as: "Store A grew, peaked in February, dipped slightly in March. Store C is the smallest but growing every month."

StoreJan 2024Feb 2024Mar 2024
A — Downtown$580$620$595
B — Campus$310$290$270
C — Suburban$165$210$245

Adding April means adding a column. Plotting the three stores as three lines is impossible without first unpivoting — a chart engine needs the month to be a value, not a column name.

Figure 1. The same nine numbers in wide and long shape. Tab between them. The wide table reads more easily; the long table connects to everything downstream — charts, regressions, dashboard filters, joins to other long tables.

Figure 1 makes the trade-off concrete. The wide tab is easy for the eye. The long tab is easy for the machine — by which we mean the chart engine, the statistical model, the dashboard filter, the join to another time-indexed table. In R, the verbs that move between these shapes are pivot_wider() and pivot_longer(); in Python's pandas they are .pivot() and .melt(); in SQL they are sometimes PIVOT / UNPIVOT and more often a UNION ALL of one query per column. The mechanics differ; the conceptual move is the same: take information that is encoded as column position and turn it into information encoded as cell value, or vice versa.

A nine-cell teaching table makes the shapes clear; a real file makes the stakes clear. Figure 2 is Zillow's state home-value extract, which arrives with one column per month.

Wide extract

51 state rows x 316 monthly value columns

State2020-012021-012022-012023-012024-012025-012026-01
California$559k$616k$714k$737k$769k$793k$778k
Texas$223k$242k$286k$313k$311k$310k$303k
Florida$252k$276k$339k$392k$402k$396k$377k
New York$348k$374k$412k$431k$452k$483k$503k

Long extract

4 wide rows -> 28 state-month rows

StateMonthZHVI
California2020-01$559k
California2021-01$616k
California2022-01$714k
California2023-01$737k
California2024-01$769k
California2025-01$793k
California2026-01$778k
Texas2020-01$223k
Texas2021-01$242k
Texas2022-01$286k
Texas2023-01$313k
Texas2024-01$311k
Texas2025-01$310k
Texas2026-01$303k
Florida2020-01$252k
Florida2021-01$276k
Florida2022-01$339k
Florida2023-01$392k
Florida2024-01$402k
Florida2025-01$396k
Florida2026-01$377k
New York2020-01$348k
New York2021-01$374k
New York2022-01$412k
New York2023-01$431k
New York2024-01$452k
New York2025-01$483k
New York2026-01$503k

Once long, the chart is direct: x = month, y = index, one line per state.

100120140160CaliforniaTexasFloridaNew YorkJanuary 2020 = 100
Figure 2. The Zillow state file in both shapes, and the chart that only the second one supports. Wide, every new month is a new column, so a dashboard built on column names breaks the month it ships. Long, a new month is a new row and nothing downstream changes. California is highlighted; all four series are indexed to January 2020 = 100.

Store it long, display it wide

The rule of thumb is to store data in long form, display it in wide form. The long form is the database-friendly shape: it is extensible (adding a period is a row, not a schema change), it is the natural input to plotting and modeling, and it is the format that joins cleanly to other long-form tables. The wide form is the presentation-friendly shape: at the very end of an analysis, when a manager wants to see a grid on a slide, you pivot the long form wider. The work of analysis happens in long; the work of communication happens in wide.

The opposite mistake — long when wide is wanted — is much rarer and almost always cheap to fix. A manager who wants to see the three months side by side, with stores down the side, is asking for a wide presentation table. Pivoting nine long rows back to three wide rows is one verb in any modern data tool. The cost of starting from long and pivoting wider when needed is essentially zero. The cost of starting from wide and pivoting longer every time something downstream needs it is paid every analysis.

Which shape to store is the only part of this that is a decision, and Figure 3 settles it by asking who the consumer is.

Store it long, display it wide

Which consumer wants which shapeA long table, one row per state-month, is what charts, models, dashboard filters, and joins all want. A wide table, one column per month, is what a human reading a page wants. Storing long and pivoting to wide at the last step serves both; storing wide serves only the second.STORE THIS ONELongone row per state-monthPIVOTWideone column per monthAS ISWANTED BY· A human reading a table· A spreadsheet· A printed reportWANTED BY· Charts· Models· Dashboard filters· Joins to other tables

Wide is a presentation, not a storage decision. Pivoting long to wide is one line; recovering long from wide means parsing column names, and the month a dashboard broke is usually the month a new one arrived.

Figure 3. Wide is a presentation, not a storage decision. Every downstream consumer except a human eye wants long, and pivoting long to wide is one line — while recovering long from wide means parsing column names, which is why the month a dashboard broke is usually the month a new column arrived.