All lessons

Module 1: Getting started · Lesson 1.4 · about 15 min

DataFrames: a table in Python

A DataFrame is a table of named columns sharing one index. Build one from a dictionary, look at its shape, columns and dtypes, pick a column, and add one.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 1 · about 15 minutes · uses Lesson 1.3. You’ll be able to build a DataFrame, describe its structure, and add a column.

A DataFrame is a table of Series that share one index: pick a column and you get a Series back, add a Series and you get a new column.

In everyday terms: a filing cabinet where each drawer is a column and each folder position is a row label. Pull out one drawer (a column), or look across all drawers at one position (a row).

1. Columns that share an index

A DataFrame is a table: named columns (each a Series), all sharing one index of row labels. The quickest way to make a small one is a dictionary of lists.

Python

2. Its structure

Python

str means text (pandas 3 stores text as its own string type), int64 whole numbers, float64 decimals.

3. A column in, a column out

Python

df['sales'] is a Series, so everything from the last lesson works on it. Assigning to a new name adds a column.

df.shape
(rows, columns).
df.columns, df.index
Column names and row labels.
df['qty']
One column as a Series.
df['total'] = ...
A new column, computed for every row.
Physical meaning
Each row is one thing (an order line), each column one fact about it (qty, price). Keeping that shape is what lets every later step, from grouping to joining, work.

“df[‘qty’] and df[[‘qty’]] are the same.” One bracket gives a Series (one column of values); two brackets give a DataFrame with one column. Some methods need one, some the other.

Watch it happen, one step at a time:

A table.
storeqtyprice
0S01218.99
1WEB124.99
2S0134.5
Shape. 3 rows, 3 columns.
A column out. df['qty'] → the Series 2, 1, 3.
A column in. df['total'] = df['qty'] * df['price']
storeqtypricetotal
0S01218.9937.98
1WEB124.9924.99
2S0134.513.5

4. Your turn

Your turn
Your turn
How it works

Principle: a DataFrame is a set of Series (columns) glued to one shared index. Column operations are vectorised; row-by-row work is slow and rarely needed.

Picking columns: df['sales'] (one name) gives a Series; df[['store', 'sales']] (a list of names) gives a DataFrame. df.sales also works for simple names, but brackets always work, even for names with spaces, so this course uses them.

Copy-on-write (pandas 3): taking part of a DataFrame and changing it never changes the original by accident. To change the original, assign to it directly (df['x'] = ... or df.loc[...] = ...).

5. Practice

  1. How do you list a DataFrame’s column names?
  2. What’s the difference between df['sales'] and df[['sales']]?
  3. Write the code to add df['sales_k'] with sales in thousands.
Answers
  1. list(df.columns) (or df.columns).
  2. The first is a Series; the second is a DataFrame with one column.
  3. df['sales_k'] = df['sales'] / 1000.

New words: DataFrame, column, shape, dtypes, copy-on-write.

  • A DataFrame is columns (Series) sharing one index.
  • df[‘col’] gives a Series; df[[‘a’, ‘b’]] gives a DataFrame.
  • Assigning to a new name adds a column computed for every row.
  • shape, columns, index and dtypes describe the structure.

Check yourself

1df['qty'] returns what?
2df.shape is (9717, 8). What does that mean?
3How do you add a column 'total' = qty × price?

Sources

Your ticks stay in this browser. Save them as a file on the course home.

Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.