pandas 1.4.2

ParametersReturnsBackRef
swaplevel(self, i=-2, j=-1, copy=True) -> 'Series'

Default is to swap the two innermost levels of the index.

Parameters

i, j : int or str

Levels of the indices to be swapped. Can pass level name as string.

copy : bool, default True

Whether to copy underlying data.

Returns

Series

Series with levels swapped in MultiIndex.

Swap levels i and j in a MultiIndex .

Examples

This example is valid syntax, but we were not able to check execution
>>> s = pd.Series(
...  ["A", "B", "A", "C"],
...  index=[
...  ["Final exam", "Final exam", "Coursework", "Coursework"],
...  ["History", "Geography", "History", "Geography"],
...  ["January", "February", "March", "April"],
...  ],
... )
... s Final exam History January A Geography February B Coursework History March A Geography April C dtype: object

In the following example, we will swap the levels of the indices. Here, we will swap the levels column-wise, but levels can be swapped row-wise in a similar manner. Note that column-wise is the default behaviour. By not supplying any arguments for i and j, we swap the last and second to last indices.

This example is valid syntax, but we were not able to check execution
>>> s.swaplevel()
Final exam  January     History         A
            February    Geography       B
Coursework  March       History         A
            April       Geography       C
dtype: object

By supplying one argument, we can choose which index to swap the last index with. We can for example swap the first index with the last one as follows.

This example is valid syntax, but we were not able to check execution
>>> s.swaplevel(0)
January     History     Final exam      A
February    Geography   Final exam      B
March       History     Coursework      A
April       Geography   Coursework      C
dtype: object

We can also define explicitly which indices we want to swap by supplying values for both i and j. Here, we for example swap the first and second indices.

This example is valid syntax, but we were not able to check execution
>>> s.swaplevel(0, 1)
History     Final exam  January         A
Geography   Final exam  February        B
History     Coursework  March           A
Geography   Coursework  April           C
dtype: object
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.indexes.multi.MultiIndex.swaplevel

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/series.py#3935
type: <class 'function'>
Commit: