Start studying
Module 6 · Data Wrangling Module contents
Module 6  ·  Card 7 / 14beginner

Series and DataFrames

A DataFrame is the pandas table: named columns that can each hold a different type of value.

Each column is a Series, which is one type of value with a label for every row, called the index.

For example, a sales DataFrame might hold a text column for product, a date column and a number column for price.

pandas reads CSV files, spreadsheets and database tables into a DataFrame with a single function call.

Unlike a NumPy array, a DataFrame mixes types across columns and keeps labels for its rows and columns.

Learn in depth ↓
Module 06 · Data Wrangling · 14 concepts
In depth  /  15 lines

Series and DataFrames, in more detail

pandas brought the data frame to Python, an idea it borrowed from the R language.

A DataFrame is built from columns, and each column is stored as a Series.

Underneath, most columns are NumPy arrays, which is why column maths is fast.

The index labels the rows; by default it counts 0, 1, 2 and so on, but it can hold dates or IDs instead.

Selecting one column, as in sales["price"], returns a Series, while a list of columns returns a DataFrame.

Arithmetic between two Series lines values up by index label, not by position.

Where the labels of two Series do not match, the result is NaN, the pandas marker for a number that is not there.

The read_csv function is the usual way in, and it guesses each column's type from the file.

The head method shows the first rows, info shows column types and counts, and describe gives summary statistics.

A DataFrame lives in memory, so a file larger than the computer's memory will not load in one go.

Reading in chunks, loading only the columns you need, or using smaller types are the usual fixes.

Many operations return a new DataFrame rather than changing the old one, so the result must be assigned back.

Calling sales.dropna() on a line of its own, for example, leaves sales unchanged.

Most Python data and charting libraries accept DataFrames directly.

A common confusion is between the index and an ordinary column: the index labels the rows and is not a column itself.

Want the full technical article? Wikipedia ↗
Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
An analyst loads a sales file with product names, dates and prices. Why use a pandas DataFrame rather than a single NumPy array?
Data Wrangling

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close