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.
Module 1 · about 15 minutes · uses Lesson 1.3. You’ll be able to build a DataFrame, describe its structure, and add a 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.
2. Its structure
str means text (pandas 3 stores text as its own string type), int64 whole numbers, float64 decimals.
3. A column in, a column out
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:
| store | qty | price | |
|---|---|---|---|
| 0 | S01 | 2 | 18.99 |
| 1 | WEB | 1 | 24.99 |
| 2 | S01 | 3 | 4.5 |
df['qty'] → the Series 2, 1, 3.df['total'] = df['qty'] * df['price']| store | qty | price | total | |
|---|---|---|---|---|
| 0 | S01 | 2 | 18.99 | 37.98 |
| 1 | WEB | 1 | 24.99 | 24.99 |
| 2 | S01 | 3 | 4.5 | 13.5 |
4. 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
- How do you list a DataFrame’s column names?
- What’s the difference between
df['sales']anddf[['sales']]? - Write the code to add
df['sales_k']with sales in thousands.
Answers
list(df.columns)(ordf.columns).- The first is a Series; the second is a DataFrame with one column.
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
Sources
- pandas documentation, User Guide: Intro to data structures https://pandas.pydata.org/docs/user_guide/dsintro.html
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
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.