diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index b123e381f082a..de8006d2168af 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -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: diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index a6b1815c8c257..5bcb8078870bf 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -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"]