- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.iloc#
- propertyDataFrame.iloc[source]#
Purely integer-location based indexing for selection by position.
Deprecated since version 2.2.0:Returning a tuple from a callable is deprecated.
.iloc[]is primarily integer position based (from0tolength-1of the axis), but may also be used with a booleanarray.Allowed inputs are:
An integer, e.g.
5.A list or array of integers, e.g.
[4,3,0].A slice object with ints, e.g.
1:7.A boolean array.
A
callablefunction with one argument (the calling Series orDataFrame) and that returns valid output for indexing (one of the above).This is useful in method chains, when you don’t have a reference to thecalling object, but would like to base your selection onsome value.A tuple of row and column indexes. The tuple elements consist of one of theabove inputs, e.g.
(0,1).
.ilocwill raiseIndexErrorif a requested indexer isout-of-bounds, exceptslice indexers which allow out-of-boundsindexing (this conforms with python/numpyslice semantics).See more atSelection by Position.
See also
DataFrame.iatFast integer location scalar accessor.
DataFrame.locPurely label-location based indexer for selection by label.
Series.ilocPurely integer-location based indexing for selection by position.
Examples
>>>mydict=[{'a':1,'b':2,'c':3,'d':4},...{'a':100,'b':200,'c':300,'d':400},...{'a':1000,'b':2000,'c':3000,'d':4000}]>>>df=pd.DataFrame(mydict)>>>df a b c d0 1 2 3 41 100 200 300 4002 1000 2000 3000 4000
Indexing just the rows
With a scalar integer.
>>>type(df.iloc[0])<class 'pandas.core.series.Series'>>>>df.iloc[0]a 1b 2c 3d 4Name: 0, dtype: int64
With a list of integers.
>>>df.iloc[[0]] a b c d0 1 2 3 4>>>type(df.iloc[[0]])<class 'pandas.core.frame.DataFrame'>
>>>df.iloc[[0,1]] a b c d0 1 2 3 41 100 200 300 400
With aslice object.
>>>df.iloc[:3] a b c d0 1 2 3 41 100 200 300 4002 1000 2000 3000 4000
With a boolean mask the same length as the index.
>>>df.iloc[[True,False,True]] a b c d0 1 2 3 42 1000 2000 3000 4000
With a callable, useful in method chains. Thex passedto the
lambdais the DataFrame being sliced. This selectsthe rows whose index label even.>>>df.iloc[lambdax:x.index%2==0] a b c d0 1 2 3 42 1000 2000 3000 4000
Indexing both axes
You can mix the indexer types for the index and columns. Use
:toselect the entire axis.With scalar integers.
>>>df.iloc[0,1]2
With lists of integers.
>>>df.iloc[[0,2],[1,3]] b d0 2 42 2000 4000
Withslice objects.
>>>df.iloc[1:3,0:3] a b c1 100 200 3002 1000 2000 3000
With a boolean array whose length matches the columns.
>>>df.iloc[:,[True,False,True,False]] a c0 1 31 100 3002 1000 3000
With a callable function that expects the Series or DataFrame.
>>>df.iloc[:,lambdadf:[0,2]] a c0 1 31 100 3002 1000 3000