2.1 Different Choices for Indexing
New in version 0.11.0.
Object selection has had a number of user-requested additions in order to support more explicit location based indexing. pandas now supports three types of multi-axis indexing.
.locis primarily label based, but may also be used with a boolean array..locwill raiseKeyErrorwhen the items are not found. Allowed inputs are:A single label, e.g.
5or'a', (note that5is interpreted as a label of the index. This use is not an integer position along the index)A list or array of labels
['a', 'b', 'c']A slice object with labels
'a':'f', (note that contrary to usual python slices, both the start and the stop are included!)A boolean array
A
callablefunction with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing (one of the above)New in version 0.18.1.
See more at Selection by Label
.ilocis primarily integer position based (from0tolength-1of the axis), but may also be used with a boolean array..ilocwill raiseIndexErrorif a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing. (this conforms with python/numpy slice semantics). Allowed inputs are:An integer e.g.
5A list or array of integers
[4, 3, 0]A slice object with ints
1:7A boolean array
A
callablefunction with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing (one of the above)New in version 0.18.1.
See more at Selection by Position
.ixsupports mixed integer and label based access. It is primarily label based, but will fall back to integer positional access unless the corresponding axis is of integer type..ixis the most general and will support any of the inputs in.locand.iloc..ixalso supports floating point label schemes..ixis exceptionally useful when dealing with mixed positional and label based hierarchical indexes.However, when an axis is integer based, ONLY label based access and not positional access is supported. Thus, in such cases, it’s usually better to be explicit and use
.ilocor.loc.See more at Advanced Indexing and Advanced Hierarchical.
.loc,.iloc,.ixand also[]indexing can accept acallableas indexer. See more at Selection By Callable.
Getting values from an object with multi-axes selection uses the following
notation (using .loc as an example, but applies to .iloc and .ix as
well). Any of the axes accessors may be the null slice :. Axes left out of
the specification are assumed to be :. (e.g. p.loc['a'] is equiv to
p.loc['a', :, :])
| Object Type | Indexers |
|---|---|
| Series | s.loc[indexer] |
| DataFrame | df.loc[row_indexer,column_indexer] |
| Panel | p.loc[item_indexer,major_indexer,minor_indexer] |