Skip to content

Commit c28a796

Browse files
committed
Update See Also. Add more examples and specifies return type
1 parent 78f342c commit c28a796

File tree

1 file changed

+86
-6
lines changed

1 file changed

+86
-6
lines changed

pandas/core/indexing.py

+86-6
Original file line numberDiff line numberDiff line change
@@ -1438,10 +1438,10 @@ class _LocIndexer(_LocationIndexer):
14381438
--------
14391439
DateFrame.at
14401440
Access a single value for a row/column label pair
1441-
DateFrame.iat
1442-
Access a single value for a row/column pair by integer position
14431441
DateFrame.iloc
14441442
Access group of rows and columns by integer position(s)
1443+
Series.loc
1444+
Access group of values using labels
14451445
14461446
Examples
14471447
--------
@@ -1453,17 +1453,21 @@ class _LocIndexer(_LocationIndexer):
14531453
r1 0 4 1
14541454
r2 10 20 30
14551455
1456-
Single label for row (note it would be faster to use ``DateFrame.at`` in
1457-
this case)
1456+
Single label. Note this returns a Series.
14581457
14591458
>>> df.loc['r1']
14601459
c0 0
14611460
c1 4
14621461
c2 1
14631462
Name: r1, dtype: int64
14641463
1465-
Single label for row and column (note it would be faster to use
1466-
``DateFrame.at`` in this case)
1464+
List with a single label. Note using ``[[]]`` returns a DataFrame.
1465+
1466+
>>> df.loc[['r1']]
1467+
c0 c1 c2
1468+
r1 0 4 1
1469+
1470+
Single label for row and column
14671471
14681472
>>> df.loc['r0', 'c1']
14691473
2
@@ -1557,6 +1561,82 @@ class _LocIndexer(_LocationIndexer):
15571561
8 0 4 1
15581562
9 10 20 30
15591563
1564+
A number of examples using a DataFrame with a multi-index
1565+
1566+
>>> tuples = [('r0', 'bar'), ('r0', 'foo'), ('r1', 'bar'),
1567+
... ('r1', 'foo'), ('r2', 'bar'), ('r2', 'baz')]
1568+
>>> index = pd.MultiIndex.from_tuples(tuples)
1569+
>>> values = [[12,2,3], [0,4,1], [10,20,30],
1570+
... [1, 4, 1], [7, 1, 2], [16, 36, 40]]
1571+
>>> df = pd.DataFrame(values, columns=['c0', 'c1', 'c2'], index=index)
1572+
>>> df
1573+
c0 c1 c2
1574+
r0 bar 12 2 3
1575+
foo 0 4 1
1576+
r1 bar 10 20 30
1577+
foo 1 4 1
1578+
r2 bar 7 1 2
1579+
baz 16 36 40
1580+
1581+
Single label. Note this returns a DataFrame with a single index.
1582+
1583+
>>> df.loc['r0']
1584+
c0 c1 c2
1585+
bar 12 2 3
1586+
foo 0 4 1
1587+
1588+
Single index tuple. Note this returns a Series.
1589+
1590+
>>> df.loc[('r0', 'bar')]
1591+
c0 12
1592+
c1 2
1593+
c2 3
1594+
Name: (r0, bar), dtype: int64
1595+
1596+
Single label for row and column. Similar to passing in a tuple, this
1597+
returns a Series.
1598+
1599+
>>> df.loc['r0', 'foo']
1600+
c0 0
1601+
c1 4
1602+
c2 1
1603+
Name: (r0, foo), dtype: int64
1604+
1605+
Single tuple. Note using ``[[]]`` returns a DataFrame.
1606+
1607+
>>> df.loc[[('r0', 'bar')]]
1608+
c0 c1 c2
1609+
r0 bar 12 2 3
1610+
1611+
Single tuple for the index with a single label for the column
1612+
1613+
>>> df.loc[('r0', 'foo'), 'c1']
1614+
4
1615+
1616+
Boolean list
1617+
1618+
>>> df.loc[[True, False, True, False, True, True]]
1619+
c0 c1 c2
1620+
r0 bar 12 2 3
1621+
r1 bar 10 20 30
1622+
r2 bar 7 1 2
1623+
baz 16 36 40
1624+
1625+
Slice from index tuple to single label
1626+
1627+
>>> df.loc[('r0', 'foo'):'r1']
1628+
c0 c1 c2
1629+
r0 foo 0 4 1
1630+
r1 bar 10 20 30
1631+
foo 1 4 1
1632+
1633+
Slice from index tuple to index tuple
1634+
1635+
>>> df.loc[('r0', 'foo'):('r1', 'bar')]
1636+
c0 c1 c2
1637+
r0 foo 0 4 1
1638+
r1 bar 10 20 30
1639+
15601640
Raises
15611641
------
15621642
KeyError:

0 commit comments

Comments
 (0)