Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ indexing pandas objects with ``[]``:

Series, ``series[label]``, scalar value
DataFrame, ``frame[colname]``, ``Series`` corresponding to colname
DataFrame, ``frame[[colname]]``, ``DataFrame`` with columns corresponding to ``[colname]``
DataFrame, ``frame[list_of_colnames]``, ``DataFrame`` with columns corresponding to ``list_of_colnames``

The same principle applies to label-based selection with ``.loc``: a
list-like of labels preserves the corresponding axis (so
``df.loc[:, ["A"]]`` returns a ``DataFrame``, even when the list contains a
single label), while a scalar label reduces the axis when labels on that
axis are unique (so ``df.loc[:, "A"]`` typically returns a ``Series``; if
``"A"`` is duplicated on the axis, a ``DataFrame`` is returned instead).
See :ref:`indexing.label` for details.

Here we construct a simple time series data set to use for illustrating the
indexing functionality:
Expand Down
16 changes: 16 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,22 @@ class _LocIndexer(_LocationIndexer):
viper 4 5
sidewinder 7 8

Single column label. Note this returns the column as a Series.

>>> df.loc[:, "max_speed"]
cobra 1
viper 4
sidewinder 7
Name: max_speed, dtype: int64

List with a single column label. Note this returns a DataFrame.

>>> df.loc[:, ["max_speed"]]
max_speed
cobra 1
viper 4
sidewinder 7

Single label for row and column

>>> df.loc["cobra", "shield"]
Expand Down
Loading