Pandas and NumPy 5 lessons
  1. 1 What each library is for
  2. 2 Getting data in and looking at it sign in to open
  3. 3 Selecting, filtering and changing sign in to open
  4. 4 Grouping, joining and reshaping sign in to open
  5. 5 Writing code you can trust sign in to open
Course overview
Data: video · change

This lesson: 122KB

Pandas and NumPy · Lesson 1 of 5

What each library is for

Stop guessing which one to reach for.

NumPy is about arrays of numbers. Pandas is about tables with labels. Almost everything else follows from that one distinction.

A NumPy array holds one type, in a fixed shape, laid out efficiently in memory. Because of that, an operation on a million numbers happens in one step in fast compiled code rather than in a Python loop. The speed difference is not small; it is often a hundred times. This is why every data library in Python sits on top of NumPy, including pandas.

Pandas gives you two things NumPy does not: column names and an index. A DataFrame is a table where columns have names and types that can differ, and rows have labels. That is what real data looks like once it leaves a textbook.

In practice you work in pandas and drop into NumPy for the numerical parts. Reading a CSV, grouping by branch, joining two tables, handling missing values: pandas. Element wise mathematics across a whole column, random numbers, matrix work: NumPy, usually without noticing, because pandas hands it the work.

The habit to build from the first day is vectorised thinking. Whenever you find yourself writing a loop over rows, stop. There is nearly always a whole column operation that does the same thing, and it will be shorter to write, faster to run, and less likely to be wrong. Looping over a DataFrame row by row is the single clearest sign of a beginner, and it is the first thing to unlearn.

Lab — try it yourself

Write a loop that adds two lists of a million numbers, then do the same with NumPy arrays. Time both and note the difference.

Check what you learned

Create your free BvLogic ID to take the quiz and record your score.

Create your BvLogic ID
Continue to lesson 2 Up next 2. Getting data in and looking at it