liveBook · Manning

Data frame

As explained in Data Storytelling with Altair and AI

A pandas DataFrame is a two-dimensional labeled tabular data structure with rows and columns.

A pandas DataFrame is a two-dimensional labeled table: rows hold records and columns hold fields. It provides the working form for moving from raw data to a chart, whether the data describes monthly tourist arrivals, airport traffic, sports performance, or call-to-action content. section 2.2.1 section 2.1.1

Table 2.1 Arrivals at tourist arrivals establishments since 1994
Date
IT
FR
DE
PT
ES
UK
1990-01-01
2,543,920
3,185,877
325,138
1723,786
1,776,000
1990-02-01
2,871,632
3,588,879
381,539
1,885,718
2,250,000
1990-03-01
3,774,702
4,272,437
493,957
2,337,847
2,662,000
…
…
…
…
…
…
…
2019-08-01
11,649,500
13,692,822
14,570,339
2,531,809
12,893,366
8,889,049
2019-09-01
9,888,817
11,684,845
14,373,815
2,263,748
5,858,984
2019-10-01
7,692,388
10,401,793
13,780,441
1,995,942
7,455,781

This table is a DataFrame-shaped dataset: each row represents a date, while country columns hold tourist-arrival values. Missing country-year values matter, because aggregating incomplete years can produce misleading results. section 2.2.1

Start by loading a file into a DataFrame and parsing date fields correctly. For the tourist-arrivals data, parsing Date as a date makes it possible to filter a time range, extract a year, group records by year, and calculate summed arrivals before charting. section 2.2.1 section 2.4.1

Load, prepare, and chart data

Load CSV data with pandas, then use the DataFrame as the input to Altair. A basic flow is: read the file; parse Date; remove rows outside the selected years; create Year; group by Year; and calculate totals. Only after those choices are explicit should the resulting table drive a visualization. section 2.2.1 section 2.4.1 section 2.1.1

Use a separate DataFrame when chart content needs its own structured data:

  • Create a one-row text DataFrame for a chart annotation, then render its text with mark_text(). section 2.3.2
  • Reshape airport data with melt(), remove missing values, filter Portugal, and draw a line chart from the prepared DataFrame. section 2.3.2
  • Add a URL column to a sports DataFrame so selected chart categories can receive image marks. section 4.5.2
  • Create columns for call-to-action text, x positions, and image paths to drive coordinated text and image charts. section 9.2.2

A DataFrame is not only imported data. It can also be constructed directly from Python dictionaries or records when the chart needs a small supporting layer, such as an annotation, a baseline, or a next-steps panel. Keeping these values in columns lets Altair encode them consistently. section 2.3.2 section 9.2.2 section 4.5.2

Before treating totals as insight, inspect completeness and define the question. In the arrivals example, incomplete years can distort aggregation; prepared values then support a chart that is enriched with contextual evidence rather than left as an unexplained display. section 2.2.1 section 2.3.2

How Data Storytelling with Altair and AI covers it

How deep it goes
Demonstrates loading, date parsing, filtering, reshaping, missing-value removal, grouping, column creation, and direct construction without describing pandas internals.
Examples
Uses tourist-arrivals and airport datasets, a small Name-Age-City table, sports image URLs, a baseline value, and CTA text-image records.
What it assumes
Assumes Python work with pandas and Altair while explicitly showing CSV loading and direct DataFrame construction.
What you can do after
Apply it via pandas and Altair to load, prepare, construct, and supply tables for layered data-story charts.
How it connects
Connects pandas, Altair, CSV files, date parsing, aggregation, missing values, melt, mark_text, mark_image, and mark_rule.

Also explained in

Compare how 19 books explain data frame →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Storytelling with Altair and AI. The section links point to the parts of the book it draws on.