The sum() Method with Pandas Series

Let's find out how to use the floordiv operator on pandas series elements.

Try it yourself

Try executing the code below to see the result.

Press + to interact
import pandas as pd
v1 = pd.Series([0, 2, 4])
v2 = pd.Series([0, 1, 2])
out = v1 // v2
print(out.sum())

Explanation

There are a few things going on in this teaser. The first has to do with the // operator in out = v1 // v2. This is the floordivReturns an integral part of the quotient. operator in Python. Unlike the regular division, it returns an integer.

In [1]: 7/2
Out[1]: 3.5
In [2]: 7//2
Out[2]: 3

The // operator is useful when we want to calculate indices (for example, in a binary search).

The next odd thing is that we managed to ...

Get hands-on with 1400+ tech skills courses.