15
15
MultiIndex / Advanced Indexing
16
16
******************************
17
17
18
- This section covers indexing with a ``MultiIndex `` and :ref: `more advanced indexing features <indexing.index_types >`.
18
+ This section covers :ref: `indexing with a MultiIndex <advanced.hierarchical >`
19
+ and :ref: `other advanced indexing features <indexing.index_types >`.
19
20
20
21
See the :ref: `Indexing and Selecting Data <indexing >` for general indexing documentation.
21
22
@@ -37,7 +38,7 @@ Hierarchical / Multi-level indexing is very exciting as it opens the door to som
37
38
quite sophisticated data analysis and manipulation, especially for working with
38
39
higher dimensional data. In essence, it enables you to store and manipulate
39
40
data with an arbitrary number of dimensions in lower dimensional data
40
- structures like Series (1d) and DataFrame (2d).
41
+ structures like `` Series `` (1d) and `` DataFrame `` (2d).
41
42
42
43
In this section, we will show what exactly we mean by "hierarchical" indexing
43
44
and how it integrates with all of the pandas indexing functionality
@@ -83,8 +84,8 @@ to use the :meth:`MultiIndex.from_product` method:
83
84
iterables = [[' bar' , ' baz' , ' foo' , ' qux' ], [' one' , ' two' ]]
84
85
pd.MultiIndex.from_product(iterables, names = [' first' , ' second' ])
85
86
86
- As a convenience, you can pass a list of arrays directly into Series or
87
- DataFrame to construct a ``MultiIndex `` automatically:
87
+ As a convenience, you can pass a list of arrays directly into `` Series `` or
88
+ `` DataFrame `` to construct a ``MultiIndex `` automatically:
88
89
89
90
.. ipython :: python
90
91
@@ -213,8 +214,8 @@ tuples:
213
214
s + s[:- 2 ]
214
215
s + s[::2 ]
215
216
216
- `` reindex `` can be called with another `` MultiIndex ``, or even a list or array
217
- of tuples:
217
+ The :meth: ` ~DataFrame. reindex ` method of `` Series ``/`` DataFrames `` can be
218
+ called with another `` MultiIndex ``, or even a list or array of tuples:
218
219
219
220
.. ipython :: python
220
221
@@ -413,7 +414,7 @@ selecting data at a particular level of a ``MultiIndex`` easier.
413
414
# using the slicers
414
415
df.loc[(slice (None ),' one' ),:]
415
416
416
- You can also select on the columns with :meth: ` ~pandas.MultiIndex.xs `, by
417
+ You can also select on the columns with `` xs ` `, by
417
418
providing the axis argument.
418
419
419
420
.. ipython :: python
@@ -426,7 +427,7 @@ providing the axis argument.
426
427
# using the slicers
427
428
df.loc[:,(slice (None ),' one' )]
428
429
429
- :meth: ` ~pandas.MultiIndex.xs ` also allows selection with multiple keys.
430
+ `` xs ` ` also allows selection with multiple keys.
430
431
431
432
.. ipython :: python
432
433
@@ -437,7 +438,7 @@ providing the axis argument.
437
438
# using the slicers
438
439
df.loc[:,(' bar' ,' one' )]
439
440
440
- You can pass ``drop_level=False `` to :meth: ` ~pandas.MultiIndex.xs ` to retain
441
+ You can pass ``drop_level=False `` to `` xs ` ` to retain
441
442
the level that was selected.
442
443
443
444
.. ipython :: python
@@ -460,9 +461,9 @@ Compare the above with the result using ``drop_level=True`` (the default value).
460
461
Advanced reindexing and alignment
461
462
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
462
463
463
- The parameter ``level `` has been added to the `` reindex `` and `` align `` methods
464
- of pandas objects. This is useful to broadcast values across a level. For
465
- instance:
464
+ Using the parameter ``level `` in the :meth: ` ~DataFrame. reindex ` and
465
+ :meth: ` ~DataFrame.align ` methods of pandas objects is useful to broadcast
466
+ values across a level. For instance:
466
467
467
468
.. ipython :: python
468
469
@@ -480,10 +481,10 @@ instance:
480
481
df2_aligned
481
482
482
483
483
- Swapping levels with :meth: ` ~pandas.MultiIndex. swaplevel `
484
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
484
+ Swapping levels with `` swaplevel ` `
485
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
485
486
486
- The `` swaplevel `` function can switch the order of two levels:
487
+ The :meth: ` ~MultiIndex. swaplevel ` method can switch the order of two levels:
487
488
488
489
.. ipython :: python
489
490
@@ -492,21 +493,21 @@ The ``swaplevel`` function can switch the order of two levels:
492
493
493
494
.. _advanced.reorderlevels :
494
495
495
- Reordering levels with :meth: ` ~pandas.MultiIndex. reorder_levels `
496
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
496
+ Reordering levels with `` reorder_levels ` `
497
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
497
498
498
- The `` reorder_levels `` function generalizes the ``swaplevel `` function,
499
- allowing you to permute the hierarchical index levels in one step:
499
+ The :meth: ` ~MultiIndex. reorder_levels ` method generalizes the ``swaplevel ``
500
+ method, allowing you to permute the hierarchical index levels in one step:
500
501
501
502
.. ipython :: python
502
503
503
504
df[:5 ].reorder_levels([1 ,0 ], axis = 0 )
504
505
505
- Sorting a :class: ` ~pandas. MultiIndex `
506
- -------------------------------------
506
+ Sorting a `` MultiIndex ` `
507
+ ------------------------
507
508
508
- For MultiIndex-ed objects to be indexed and sliced effectively, they need
509
- to be sorted. As with any index, you can use `` sort_index ` `.
509
+ For :class: ` MultiIndex ` -ed objects to be indexed and sliced effectively,
510
+ they need to be sorted. As with any index, you can use :meth: ` ~DataFrame. sort_index `.
510
511
511
512
.. ipython :: python
512
513
@@ -658,9 +659,9 @@ faster than fancy indexing.
658
659
Index Types
659
660
-----------
660
661
661
- We have discussed ``MultiIndex `` in the previous sections pretty extensively. `` DatetimeIndex `` and `` PeriodIndex ``
662
- are shown :ref: `here <timeseries.overview >`, and information about
663
- ``TimedeltaIndex `` is found :ref: `here <timedeltas.timedeltas >`.
662
+ We have discussed ``MultiIndex `` in the previous sections pretty extensively.
663
+ Documentation about `` DatetimeIndex `` and `` PeriodIndex `` are shown :ref: `here <timeseries.overview >`,
664
+ and documentation about ``TimedeltaIndex `` is found :ref: `here <timedeltas.timedeltaindex >`.
664
665
665
666
In the following sub-sections we will highlight some other index types.
666
667
@@ -1004,8 +1005,8 @@ Non-monotonic indexes require exact matches
1004
1005
1005
1006
If the index of a `` Series`` or `` DataFrame`` is monotonically increasing or decreasing, then the bounds
1006
1007
of a label- based slice can be outside the range of the index, much like slice indexing a
1007
- normal Python `` list `` . Monotonicity of an index can be tested with the `` is_monotonic_increasing` ` and
1008
- `` is_monotonic_decreasing` ` attributes.
1008
+ normal Python `` list `` . Monotonicity of an index can be tested with the :meth: ` ~ Index. is_monotonic_increasing` and
1009
+ :meth: ` ~ Index. is_monotonic_decreasing` attributes.
1009
1010
1010
1011
.. ipython:: python
1011
1012
@@ -1039,9 +1040,9 @@ On the other hand, if the index is not monotonic, then both slice bounds must be
1039
1040
In [11 ]: df.loc[2 :3 , :]
1040
1041
KeyError : ' Cannot get right slice bound for non-unique label: 3'
1041
1042
1042
- :meth: ` Index.is_monotonic_increasing` and :meth: ` Index.is_monotonic_decreasing` only check that
1043
+ `` Index.is_monotonic_increasing`` and `` Index.is_monotonic_decreasing` ` only check that
1043
1044
an index is weakly monotonic. To check for strict monotonicity, you can combine one of those with
1044
- :meth:`Index.is_unique`
1045
+ the :meth:`~ Index.is_unique` attribute.
1045
1046
1046
1047
.. ipython:: python
1047
1048
@@ -1057,7 +1058,7 @@ Compared with standard Python sequence slicing in which the slice endpoint is
1057
1058
not inclusive, label- based slicing in pandas ** is inclusive** . The primary
1058
1059
reason for this is that it is often not possible to easily determine the
1059
1060
" successor" or next element after a particular label in an index. For example,
1060
- consider the following Series:
1061
+ consider the following `` Series`` :
1061
1062
1062
1063
.. ipython:: python
1063
1064
0 commit comments