Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Data and Figure Basics

Tidy Data: Long-Form vs Wide-Form Tables

Learn how seaborn reads long-form (tidy) and wide-form data, and how to reshape tables with melt and pivot so every plot call is simple.

Seaborn is designed around long-form (also called tidy) tables: every row is one observation and every column is one variable. It also accepts wide-form tables, where each column holds a different series. Knowing which shape you have, and how to convert between them with pandas, is the single most useful data skill for seaborn. Most "why does my plot look wrong?" questions come down to a table in the wrong shape.

Long form: one observation per row

A long-form table of monthly sales for two regions looks like this:

month region sales
Jan North 120
Jan South 95
Feb North 130
Feb South 90

Each column is a variable (month, region, sales), each row an observation. Because every variable has its own column, seaborn can map any of them to an axis, a color, a size or a facet:

Python
sns.lineplot(data=long_df, x="month", y="sales", hue="region")

This is the form seaborn's own examples use, and it is the most flexible: adding a third region adds rows, not columns, and no plotting code changes.

Wide form: one column per series

The same data in wide form has one row per month and one column per region:

month North South
Jan 120 95
Feb 130 90

Spreadsheets and CSV exports are usually wide. Seaborn can plot wide tables directly: pass the whole table as data= and each numeric column becomes a series, with the index on the x axis.

Python Wide to long with melt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two charts are identical. The advantage of the long table shows when you need a variable the wide version cannot express: coloring by region and sizing by another column, or faceting by region with col="region". In wide form, the region is hidden in the column names rather than stored as data.

melt: wide to long

DataFrame.melt stacks columns into rows:

Python
long = wide.reset_index().melt(
    id_vars="month",          # columns to keep as identifiers
    var_name="region",        # new column holding the old column names
    value_name="sales",       # new column holding the values
)
  • id_vars stay as they are and are repeated for each melted column.
  • value_vars (optional) restricts which columns to melt; the default is all others.
  • Name the new columns yourself; the defaults variable and value are unhelpful on an axis label.

pivot: long to wide

pivot and pivot_table go the other way. You need wide form for heatmaps and for correlation-style matrices:

Python Pivot for a heatmap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

pivot_table aggregates duplicates (here with aggfunc="mean"), while plain pivot raises an error if a day and hour combination appears twice.

How seaborn tells the two forms apart

Seaborn decides by the arguments you give:

  • If you pass x=, y= or hue= as strings together with data=, it treats data as long-form.
  • If you pass data= alone, it treats it as wide-form and plots each column.
  • If you pass vectors (lists, arrays, Series) to x= and y=, it uses them directly.
Python
sns.boxplot(data=wide)                       # one box per column (wide form)
sns.boxplot(data=long, x="region", y="sales")  # same plot, long form

For categorical functions, wide form means "one distribution per column", which is handy for quick looks at several measurements.

Tidy-data rules of thumb

  1. Each variable gets its own column; each observation its own row.
  2. Do not store values in column names (2022, 2023, 2024 as columns are really a year variable).
  3. Do not pack two variables into one column ("North-Jan"); split them.
  4. Keep one measurement type per column, so units stay consistent.
  5. When a plot needs a matrix (heatmap, clustermap), pivot at the last moment, and keep the tidy table as your source of truth.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In long-form data, what does each row represent?

    Long (tidy) form stores one observation per row and one variable per column.
  2. 2. Which pandas method turns the wide columns North, South and West into one 'region' column and one 'sales' column?

    melt unpivots columns into rows. var_name and value_name name the two new columns.
  3. 3. How does seaborn treat sns.boxplot(data=df) with no x or y?

    Without x and y, seaborn assumes wide-form data and draws each numeric column as its own series.
  4. 4. You need a day-by-hour matrix of averages for a heatmap. What do you use?

    pivot_table reshapes long data into a matrix and aggregates repeated combinations.

Summary

  • Seaborn prefers long-form (tidy) tables: one row per observation, one column per variable.
  • Wide-form tables can be plotted by passing data= alone, but they cannot express extra variables such as a color or facet.
  • melt converts wide to long; pivot and pivot_table convert long to wide.
  • Heatmaps and clustermaps need matrices, so pivot just before plotting.
  • Do not hide variables in column names.

Next: Datasets and load_dataset