Label-oriented indexing

The .loc operator supports pure label-based indexing. It accepts the following as valid inputs:

  • A single label such as ["colC"], [2], or ["R1"]—note that in cases where the label is an integer, it doesn't refer to the integer position of the index, but the integer is itself a label.
  • A list or array of labels, for example, ["colA", "colB" ].
  • A slice object with labels, for example, "colB":"colD".
  • A Boolean array.

Let's examine each of these four cases with respect to the following two Series—one with an integer-based label and another with a string-based label:

ser_loc1 = pd.Series(np.linspace(11, 15, 5))ser_loc2 = pd.Series(np.linspace(11, 15, 5), index = list("abcde"))# Indexing with single labelIn: ser_loc1.loc[2] ...

Get Mastering pandas - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.