Chapter 2 · Section 1

DataFrames and the First Look

Prof. Xuhu Wan

§1 — DataFrames and the First Look

Identify what a row represents, load a table, and inspect its structure before analysing it.

Cross-sectional Data versus Time Series

First ask: what does one row represent?

Cross-sectional data Time-series data
Compare Different entities in the same period The same entity or measure over time
Example One row per customer this month One row per day of a store’s sales
Row order Usually no natural time sequence Chronological order matters
Missing values Consider comparable entities and the variable’s distribution Respect time and what was known then
Train/test split Random split can suit independent entities For forecasting: earlier train, later test

Our order-line table contains transactions. Aggregating sales by day creates a time series.

A Column Is a Series

A Series is one labelled variable. A DataFrame places related Series in columns and gives them a shared row index.

Rows, Columns, and the Index

Read across a row for one observation. Read down a column for one variable. Index labels identify rows; numerical positions count from 0.

DataFrame diagram with row labels and several Series columns

Create a Small DataFrame

pd.DataFrame({...}) uses dictionary keys as column names and lists as column values. Supply an index= when meaningful row labels are available.

Change one price, then run the box again. Check which cell changes.

Load a CSV File

A CSV stores a table as text separated by commas. pd.read_csv() returns a DataFrame. The first line normally supplies column names.

DataFrame Attributes

Attributes describe the table and have no parentheses. .shape is (rows, columns); .columns and .index contain labels; .dtypes gives one data type per column.

DataFrame Methods

Methods perform a task and use parentheses. .head(2) returns the first two rows. .tail(2) returns the last two. .info() prints a structural summary.

.info() prints its report; avoid wrapping it in print(...), which would also print its return value, None.

Column Types and Business Meaning

Quantity is numerical, Store is categorical, and Date initially contains text. A number-like code may still be an identifier. Data type and business meaning both matter.

In-Class Practice: Inspect an Order Table

Print the row count, return the final two records, and name the column containing unit prices. Explain whether the index identifies stores or orders.

Hint: .shape[0], .tail(2), and .columns. Expected: four rows, last labels C and D, prices in Price.