Pandas and NumPy
Lesson 1 of 5 5 lessons · free
Pandas and NumPy 5 lessons
  1. 1 What each library is for
  2. 2 Getting data in and looking at it
  3. 3 Selecting, filtering and changing
  4. 4 Grouping, joining and reshaping
  5. 5 Writing code you can trust
  1. ✎ Course lab6 tasks · marked out of 100
Course overview
Data: video · change

This lesson: 356KB

Pandas and NumPy · Lesson 1 of 5

What each library is for

Stop guessing which one to reach for.

That was a one-minute preview. The full course video is for signed-in learners. Sign in to watch it here, or create a free BvLogic ID.

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.

Exercise: 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

Up next: 2. Getting data in and looking at it

Continue to lesson 2 Up next 2. Getting data in and looking at it