Puzzle 7 A Delicious Div Sum
Pandas Brain Teasers — by Miki Tebeka (15 / 34)
👈 Full of It | TOC | Once Upon a Time 👉
import pandas as pd
v1 = pd.Series([0, 2, 4])
v2 = pd.Series([0, 1, 2])
out = v1 // v2
print(out.sum())
Guess the Output
IMPORTANT
Try to guess what the output is before moving to the next page.
This code will print: 4.0
There are a few things going on in this teaser. The first is the // operator in out = v1 // v2. This is the floordiv operator in Python. Unlike the regular division, it returns an int.
In [1]: 7/2
Out[1]: 3.5
In [2]: 7//2
Out[2]: 3
The // operator is useful when you want to calculate indices (e.g., in a binary search).
The next odd thing is that we managed to divide by 0. If you try to divide by 0 in the Python shell, it’ll fail:
In [3]: 1/0
...
ZeroDivisionError: division by zero