Skip to content

Commit f49355d

Browse files
tuhinmahmudjreback
authored andcommitted
DOC:Improve the docstring of DataFrame.iloc() (pandas-dev#20228)
1 parent eb95979 commit f49355d

File tree

1 file changed

+115
-3
lines changed

1 file changed

+115
-3
lines changed

pandas/core/indexing.py

+115-3
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,8 @@ def _getitem_axis(self, key, axis=None):
19241924

19251925

19261926
class _iLocIndexer(_LocationIndexer):
1927-
"""Purely integer-location based indexing for selection by position.
1927+
"""
1928+
Purely integer-location based indexing for selection by position.
19281929
19291930
``.iloc[]`` is primarily integer position based (from ``0`` to
19301931
``length-1`` of the axis), but may also be used with a boolean
@@ -1937,14 +1938,125 @@ class _iLocIndexer(_LocationIndexer):
19371938
- A slice object with ints, e.g. ``1:7``.
19381939
- A boolean array.
19391940
- A ``callable`` function with one argument (the calling Series, DataFrame
1940-
or Panel) and that returns valid output for indexing (one of the above)
1941+
or Panel) and that returns valid output for indexing (one of the above).
1942+
This is useful in method chains, when you don't have a reference to the
1943+
calling object, but would like to base your selection on some value.
19411944
19421945
``.iloc`` will raise ``IndexError`` if a requested indexer is
19431946
out-of-bounds, except *slice* indexers which allow out-of-bounds
19441947
indexing (this conforms with python/numpy *slice* semantics).
19451948
1946-
See more at :ref:`Selection by Position <indexing.integer>`
1949+
See more at ref:`Selection by Position <indexing.integer>`.
1950+
1951+
See Also
1952+
--------
1953+
DataFrame.iat : Fast integer location scalar accessor.
1954+
DataFrame.loc : Purely label-location based indexer for selection by label.
1955+
Series.iloc : Purely integer-location based indexing for
1956+
selection by position.
1957+
1958+
Examples
1959+
--------
1960+
1961+
>>> mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
1962+
... {'a': 100, 'b': 200, 'c': 300, 'd': 400},
1963+
... {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
1964+
>>> df = pd.DataFrame(mydict)
1965+
>>> df
1966+
a b c d
1967+
0 1 2 3 4
1968+
1 100 200 300 400
1969+
2 1000 2000 3000 4000
1970+
1971+
**Indexing just the rows**
1972+
1973+
With a scalar integer.
1974+
1975+
>>> type(df.iloc[0])
1976+
<class 'pandas.core.series.Series'>
1977+
>>> df.iloc[0]
1978+
a 1
1979+
b 2
1980+
c 3
1981+
d 4
1982+
Name: 0, dtype: int64
1983+
1984+
With a list of integers.
1985+
1986+
>>> df.iloc[[0]]
1987+
a b c d
1988+
0 1 2 3 4
1989+
>>> type(df.iloc[[0]])
1990+
<class 'pandas.core.frame.DataFrame'>
1991+
1992+
>>> df.iloc[[0, 1]]
1993+
a b c d
1994+
0 1 2 3 4
1995+
1 100 200 300 400
1996+
1997+
With a `slice` object.
1998+
1999+
>>> df.iloc[:3]
2000+
a b c d
2001+
0 1 2 3 4
2002+
1 100 200 300 400
2003+
2 1000 2000 3000 4000
2004+
2005+
With a boolean mask the same length as the index.
2006+
2007+
>>> df.iloc[[True, False, True]]
2008+
a b c d
2009+
0 1 2 3 4
2010+
2 1000 2000 3000 4000
2011+
2012+
With a callable, useful in method chains. The `x` passed
2013+
to the ``lambda`` is the DataFrame being sliced. This selects
2014+
the rows whose index label even.
2015+
2016+
>>> df.iloc[lambda x: x.index % 2 == 0]
2017+
a b c d
2018+
0 1 2 3 4
2019+
2 1000 2000 3000 4000
2020+
2021+
**Indexing both axes**
2022+
2023+
You can mix the indexer types for the index and columns. Use ``:`` to
2024+
select the entire axis.
2025+
2026+
With scalar integers.
2027+
2028+
>>> df.iloc[0, 1]
2029+
2
2030+
2031+
With lists of integers.
2032+
2033+
>>> df.iloc[[0, 2], [1, 3]]
2034+
b d
2035+
0 2 4
2036+
2 2000 4000
2037+
2038+
With `slice` objects.
2039+
2040+
>>> df.iloc[1:3, 0:3]
2041+
a b c
2042+
1 100 200 300
2043+
2 1000 2000 3000
2044+
2045+
With a boolean array whose length matches the columns.
2046+
2047+
>>> df.iloc[:, [True, False, True, False]]
2048+
a c
2049+
0 1 3
2050+
1 100 300
2051+
2 1000 3000
2052+
2053+
With a callable function that expects the Series or DataFrame.
19472054
2055+
>>> df.iloc[:, lambda df: [0, 2]]
2056+
a c
2057+
0 1 3
2058+
1 100 300
2059+
2 1000 3000
19482060
"""
19492061

19502062
_valid_types = ("integer, integer slice (START point is INCLUDED, END "

0 commit comments

Comments
 (0)