pandas basics · lesson 2 of 9
Parts in this lesson
Apd.DataFrame({...})a table from a dictionary of columns
Bdf["kilos"]one column: a Series
Cdf.info()columns, types and gaps
Ddf.describe()a summary of each numeric column

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, info and describe

A table of Series

Run the cells.

build.ipynb

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.DictReader gave 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 parts of a DataFrame. A DataFrame is a table: rows and named columns. Here are the first four rows of the sales file.0123storekilospriceBatangas4245.0Lipa3546.0Tanauan2945.0Batangas3845.0strint64float64
1 of 5 A DataFrame is a table: rows and named columns. Here are the first four rows of the sales file.

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.

first-look.ipynb
  • head() shows the first five rows, tail(3) the last three. Never print a whole table of 100,000 rows to look at it.
  • shape is (9, 4): rows, then columns.
  • dtypes gives each column's type. kilos is int64 and price float64, as they should be. date and store are str, text. (Older pandas says object here. 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.

describe.ipynb

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

A table has shape (1200, 8). What does that mean?
df.info() says a column has 950 non-null values in a table of 1,000 rows. What does that tell you?
You run df.describe() and the price column isn’t in it. What’s the most likely reason?

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.

All sources for Simple Data Analysis