w3resource

Pandas DataFrame property: loc

DataFrame - loc property

The loc property is used to access a group of rows and columns by label(s) or a boolean array.

.loc[] is primarily label based, but may also be used with a boolean array.

Allowed inputs are:

  • A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index, and never as an integer position along the index).
  • A list or array of labels, e.g. ['a', 'b', 'c'].
  • A slice object with labels, e.g. 'a':'f'.
  • A boolean array of the same length as the axis being sliced, e.g. [True, False, True].
  • A callable function with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above)
Syntax:

DataFrame.loc

Raises: KeyError
when any items are not found

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - iat property
Next: DataFrame - iloc property



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/dataframe/dataframe-loc.php