Skip to content

Commit 1a93d2a

Browse files
committed
More labels for examples. More examples. Update wording based on PR comments
1 parent 2f359b9 commit 1a93d2a

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed

pandas/core/indexing.py

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,9 +1417,7 @@ class _LocIndexer(_LocationIndexer):
14171417
Selects a group of rows and columns by label(s) or a boolean array.
14181418
14191419
``.loc[]`` is primarily label based, but may also be used with a
1420-
boolean array. Note that if no row or column labels are specified
1421-
the labels will default to the integers 0 to n - 1, with n being
1422-
the number of rows/columns, respectively.
1420+
boolean array.
14231421
14241422
Allowed inputs are:
14251423
@@ -1429,19 +1427,19 @@ class _LocIndexer(_LocationIndexer):
14291427
- A list or array of labels, e.g. ``['a', 'b', 'c']``.
14301428
- A slice object with labels, e.g. ``'a':'f'`` (note that contrary
14311429
to usual python slices, **both** the start and the stop are included!).
1432-
- A boolean array, e.g. [True, False, True].
1430+
- A boolean array of the same length as the axis being sliced,
1431+
e.g. ``[True, False, True]``.
14331432
- A ``callable`` function with one argument (the calling Series, DataFrame
14341433
or Panel) and that returns valid output for indexing (one of the above)
14351434
1436-
``.loc`` will raise a ``KeyError`` when the items are not found.
1437-
14381435
See more at :ref:`Selection by Label <indexing.label>`
14391436
14401437
See Also
14411438
--------
1442-
at : Selects a single value for a row/column label pair
1443-
iat : Selects a single value for a row/column pair by integer position
1444-
iloc : Selects group of rows and columns by integer position(s)
1439+
DateFrame.at : Access a single value for a row/column label pair
1440+
DateFrame.iat : Access a single value for a row/column pair by integer
1441+
position
1442+
DateFrame.iloc : Access group of rows and columns by integer position(s)
14451443
14461444
Examples
14471445
--------
@@ -1452,30 +1450,82 @@ class _LocIndexer(_LocationIndexer):
14521450
r0 12 2 3
14531451
r1 0 4 1
14541452
r2 10 20 30
1453+
1454+
Single label for row (note it would be faster to use ``DateFrame.at`` in
1455+
this case)
1456+
14551457
>>> df.loc['r1']
14561458
c0 0
14571459
c1 4
14581460
c2 1
14591461
Name: r1, dtype: int64
1462+
1463+
1464+
Single label for row and column (note it would be faster to use
1465+
``DateFrame.at`` in this case)
1466+
1467+
>>> df.loc['r0', 'c1']
1468+
2
1469+
1470+
1471+
A list of labels
1472+
14601473
>>> df.loc[['r1', 'r2']]
14611474
c0 c1 c2
14621475
r1 0 4 1
14631476
r2 10 20 30
1464-
>>> df.loc['r0', 'c1']
1465-
2
1477+
1478+
Slice with labels for row and single label for column. Note that
1479+
contrary to usual python slices, both the start and the stop are
1480+
included!
1481+
14661482
>>> df.loc['r0':'r1', 'c0']
14671483
r0 12
14681484
r1 0
14691485
Name: c0, dtype: int64
1486+
1487+
1488+
Boolean list with the same length as the row axis
1489+
14701490
>>> df.loc[[False, False, True]]
14711491
c0 c1 c2
14721492
r2 10 20 30
1493+
1494+
Callable that returns valid output for indexing
1495+
14731496
>>> df.loc[df['c1'] > 10]
14741497
c0 c1 c2
14751498
r2 10 20 30
1499+
1500+
Callable that returns valid output with column labels specified
1501+
14761502
>>> df.loc[df['c1'] > 10, ['c0', 'c2']]
14771503
c0 c2
14781504
r2 10 30
1505+
1506+
Another example using integers for the index
1507+
1508+
>>> df = pd.DataFrame([[12, 2, 3], [0, 4, 1], [10, 20, 30]],
1509+
... index=[7, 8, 9], columns=['c0', 'c1', 'c2'])
1510+
>>> df
1511+
c0 c1 c2
1512+
7 12 2 3
1513+
8 0 4 1
1514+
9 10 20 30
1515+
1516+
Slice with integer labels for rows. Note that contrary to usual
1517+
python slices, both the start and the stop are included!
1518+
1519+
>>> df.loc[7:9]
1520+
c0 c1 c2
1521+
7 12 2 3
1522+
8 0 4 1
1523+
9 10 20 30
1524+
1525+
Raises
1526+
------
1527+
KeyError:
1528+
when items are not found
14791529
"""
14801530

14811531
_valid_types = ("labels (MUST BE IN THE INDEX), slices of labels (BOTH "

0 commit comments

Comments
 (0)