Print the Length of DataFrames at a Specified Location

Let's find out how length and slicing operations work in pandas DataFrames.

We'll cover the following

Try it yourself

Try executing the code below to see the result.

Press + to interact
import pandas as pd
df = pd.DataFrame([
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
])
print(len(df.loc[1:3]))

Explanation

Slices in Python are half-open rangesA half-open range includes the first element but excludes the last one.. We get values from the first index up to, but not including, the last index:

In [1]: chars = ['a', 'b', 'c', 'd', 'e']
In [2]: chars[1:3]
Out[2]: ['b', 'c']

Most of the time, pandas works in the same way, which we can see below:

In [3]: s = pd.Series(chars)
In [4]: s[1:3]
Out[4]:
1 b
2 c
dtype: object

Here are three different ways to slice a pandas.Series or a pandas.DataFrame:

  • Use loc, which works by label.
  • Use iloc, which works by offset.
  • Use a slice notation such as s[1:3], which works like iloc.

The loc function works by label, and it slices on a closed range that includes the last index, like this:

In [5]: df[1:3]
Out[5]:
  0 1 2
1 2 2 2
2 3 3 3
In [6]: df.iloc[1:3]
Out[6]:
  0 1 2
1 2 2 2
2 3 3 3
In [7]: df.loc[1:3]
Out[7]:
  0 1 2
1 2 2 2
2 3 3 3
3 4 4 4

We should remember to watch out for this off-by-one error when using .loc.

Get hands-on with 1300+ tech skills courses.