Skip to course content
Free data visualization course

Data Visualization and Dashboard Storytelling

Unit 10.01: Long-form data, and the reshape that precedes the chart

Seaborn expects long form, and spreadsheets arrive in wide form.

One row per observation

The same data as one column per series, and as one row per observation.

The code reshapes it and reports both shapes.

import pandas as pd

wide = pd.DataFrame({"month": [1, 2, 3],
                     "North": [100, 108, 115],
                     "South": [90, 92, 95]})
print("wide -- one column per series, how the spreadsheet arrives:")
print(wide.to_string(index=False))

long = wide.melt(id_vars="month", var_name="region", value_name="revenue")
print("\nlong -- one row per observation, what Seaborn expects:")
print(long.to_string(index=False))

print(f"\nwide: {wide.shape[0]} rows x {wide.shape[1]} columns")
print(f"long: {long.shape[0]} rows x {long.shape[1]} columns")

# Adding a third region means a new COLUMN in wide form and new ROWS in long
# form. Long form is why one Seaborn call handles any number of series without
# the plotting code changing.

Adding a third region means a new *column* in wide form and new *rows* in long form. That difference is why one Seaborn call handles any number of series without the plotting code changing.

It is also why the reshape usually has to happen. Data arrives wide because that is how people read it, and charting libraries want long because that is how they group.

The mistake this prevents

The mistake is reshaping repeatedly in the plotting code. Reshape once, close to where the data is loaded, and let everything downstream assume long form - otherwise the same melt appears in six places with slightly different column names.

Takeaway

Reshape to long form once, near the data load. Wide form needs new columns for new series; long form needs only new rows.