DataFrames
Last updated
A DataFrame is a table: named columns, each a Series, sharing one index. It's what you'll work with for the rest of the course. In this lesson you'll build DataFrames by hand, load one from a file, and use the handful of commands that tell you what any table holds before you do anything else with it.
By the end of this lesson you can
- Build a DataFrame from a dictionary of columns and from a list of rows
- Name the parts of a DataFrame: index, columns, values and dtypes
- Get to know a new table with
head,shape,dtypes,infoanddescribe
A table of Series
Run the cells.
There are two common ways to build a table by hand:
- A dictionary of columns: each key is a column name, each value a list. This is the first cell. Every list must be the same length.
- A list of rows: each row a dictionary, like the rows
csv.DictReadergave you in Stage 1. This is the last cell. Data from a web API usually arrives this way.
df["kilos"] takes one column out. It's a Series, and its index is the table's index: 0, 1, 2. Most of the time you won't build tables by hand. You'll read them from files, which is the next lesson, but now you know what read_csv is making.
The parts of a table
Step through the parts. You'll use these names constantly.
The index is worth a second look. When pandas reads a file, it numbers the rows 0, 1, 2… and those numbers look like positions. They're labels, as in lesson 3.1. After you sort or filter a table, label 0 might be at the bottom, or gone. Lesson 3.4 shows when that matters.
First look at a file
This is the sales file most of this stage uses: nine rows, three stores over three days. Run the cells. These five commands are the first thing to run on any table you haven't seen before.
head()shows the first five rows,tail(3)the last three. Never print a whole table of 100,000 rows to look at it.shapeis(9, 4): rows, then columns.dtypesgives each column's type.kilosisint64andpricefloat64, as they should be.dateandstorearestr, text. (Older pandas saysobjecthere. pandas 3 has a proper text type,str.)info()puts it all together: every column, how many values it has that aren't missing ("Non-Null Count"), and its type. With 9 rows and 9 non-null in every column, nothing is missing. It's the quickest way to spot gaps.
Notice date is text. pandas doesn't guess that a column is dates unless you ask; lesson 3.3 shows how.
Wrong way: the column describe left out
describe() summarises a table in one line. Run the first cell.
It's useful: the count, mean, standard deviation, smallest, largest and quartiles of each column. The middle kilos value (50%) is 34, and the standard deviation 4.53. (It's pandas, so that's ddof=1, from lesson 2.8.)
But it only shows kilos and price. date and store have gone, with no message, because by default describe only summarises numbers. If store names had been typed three ways, or kilos had been read as text, you'd never see it here. A text column you expected to be numbers silently disappears from the summary, which is exactly when you most need to see it.
The second cell asks for include="all". Text columns get their own rows: how many unique values, the most common one (top) and how often it appears (freq). Three stores, as there should be. Read the output of describe() alongside dtypes, so you know which columns it left out.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- pandas: Intro to data structures, DataFrame: building DataFrames from dictionaries and lists
- pandas: pandas.DataFrame.info: columns, non-null counts and dtypes
- pandas: pandas.DataFrame.describe: which columns it summarises, and include="all"
- McKinney, Data Structures for Statistical Computing in Python, SciPy 2010: the DataFrame as a collection of labelled columns