Loading video player…

Rounding Numbers in Python (Overview)

With many businesses turning to Python’s powerful data science ecosystem to analyze their data, understanding how to avoid introducing bias into datasets is absolutely vital. If you’ve studied some statistics, then you’re probably familiar with terms like reporting bias, selection bias, and sampling bias. There’s another type of bias that plays an important role when you’re dealing with numeric data: rounding bias.

Understanding how rounding works in Python can help you avoid biasing your dataset. This is an important skill. After all, drawing conclusions from biased data can lead to costly mistakes.

In this video course, you’ll learn:

  • Why the way you round numbers is important
  • How to round a number according to various rounding strategies
  • How to implement each strategy in pure Python
  • How rounding affects data and which rounding strategy minimizes this effect
  • How to round numbers in NumPy arrays and pandas DataFrames
  • When to apply different rounding strategies
Download

Course Slides (.pdf)

1.1 MB
Download

Sample Code (.zip)

3.7 KB

00:00 Welcome to Rounding Numbers in Python. My name is Christopher, and I will be your guide. In this course, you’ll learn about: rounding numbers, how there are multiple algorithms for rounding, the shortfalls of those multiple algorithms, how floating point messes all this up, and how the decimal class might give you something a little more precise than floating point.

00:24 The code in this course was tested with Python 3.12, but there’s nothing in here particularly new, and most of it likely would be the same, even in Python 2.

00:35 The rounding you were taught how to do in grade school likely isn’t exactly the same as what Python does. It’s close, but there are some edge cases that are probably different.

00:45 In fact, there are many different ways of rounding numbers ranging from the simple to the more complex, and unfortunately, almost all rounding algorithms will affect the shape of your data.

00:56 If your data is a bell curve centered at zero, there’s a chance the rounded version of your data is shifted left or right. This is one of the reasons there are different ways of rounding to deal with this problem.

01:09 The other complication is that floating-point numbers are not a precise numeric representation, and so when you round them, you might not see the behavior that you expect.

01:20 Okay, next up, I’ll start you on your rounding journey. You spin me right round baby, right round like a record baby. Depending on your age, you’ll have to Google both record and the British pop sensation, Dead or Alive, or you can just nod your head at the dated reference and move on.

Become a Member to join the conversation.