tml)
  * [ Resampling ](../resampling.html)
  * [ Style ](../style.html)
  * [ Plotting ](../plotting.html)
  * [ Options and settings ](../options.html)
  * [ Extensions ](../extensions.html)
  * [ Testing ](../testing.html)
  * [ Missing values ](../missing_value.html)

  * [ __ ](../../index.html)
  * [ API reference ](../index.html)
  * [ DataFrame ](../frame.html)
  * pandas.DataF... 

#  pandas.DataFrame.swaplevel  #

DataFrame.  swaplevel  (  _ i  =  -2  _ , _ j  =  -1  _ , _ axis  =  0  _ )  [
[source]  ](https://github.com/pandas-
dev/pandas/blob/v2.2.2/pandas/core/frame.py#L7758-L7834) #

    

Swap levels i and j in a [ ` MultiIndex  `
](pandas.MultiIndex.html#pandas.MultiIndex "pandas.MultiIndex") .

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.

**axis** {0 or âindexâ, 1 or âcolumnsâ}, default 0

    

The axis to swap levels on. 0 or âindexâ for row-wise, 1 or âcolumnsâ
for column-wise.

Returns  :

    

DataFrame

    

DataFrame with levels swapped in MultiIndex.

Examples

    
    
    >>> df = pd.DataFrame(
    ...     {"Grade": ["A", "B", "A", "C"]},
    ...     index=[
    ...         ["Final exam", "Final exam", "Coursework", "Coursework"],
    ...         ["History", "Geography", "History", "Geography"],
    ...         ["January", "February", "March", "April"],
    ...     ],
    ... )
    >>> df
                                        Grade
    Final exam  History     January      A
                Geography   February     B
    Coursework  History     March        A
                Geography   April        C
    

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.

    
    
    >>> df.swaplevel()
                                        Grade
    Final exam  January     History         A
                February    Geography       B
    Coursework  March       History         A
                April       Geography       C
    

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.

    
    
    >>> df.swaplevel(0)
                                        Grade
    January     History     Final exam      A
    February    Geography   Final exam      B
    March       History     Coursework      A
    April       Geography   Coursework      C
    

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.

    
    
    >>> df.swaplevel(0, 1)
                                        Grade
    History     Final exam  January         A
    Geography   Final exam  February        B
    History     Coursework  March           A
    Geography   Coursework  April           C
    

[ __ previous  pandas.DataFrame.nsmallest  ](pandas.DataFrame.nsmallest.html
"previous page") [ next  pandas.DataFrame.stack  __
](pandas.DataFrame.stack.html "next page")

__ On this page

  * ` DataFrame.swaplevel()  `

[ __ Show Source
](../../_sources/reference/api/pandas.DataFrame.swaplevel.rst.txt)

© 2024, pandas via [ NumFOCUS, Inc. ](https://numfocus.org) Hosted by [
OVHcloud ](https://www.ovhcloud.com) .

Created using [ Sphinx ](https://www.sphinx-doc.org/) 7.2.6.  

Built with the [ PyData Sphinx Theme ](https://pydata-sphinx-
theme.readthedocs.io/en/stable/index.html) 0.14.4.