Search⌘ K
AI Features

Print the Length of DataFrames at a Specified Location

Explore how to print the length of pandas DataFrames at specified locations by using different slicing methods like loc, iloc, and slice notation. Understand half-open and closed range slicing, and learn to avoid common off-by-one errors in pandas indexing.

We'll cover the following...

Try it yourself

Try executing the code below to see the result.

Python 3.8
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 ...