@@ -6278,6 +6278,54 @@ def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
62786278 Returns
62796279 -------
62806280 DataFrame
6281+
6282+ Examples
6283+ --------
6284+ >>> df = pd.DataFrame({'Grade':['A','B','A','C']},
6285+ ... index=[['Final exam', 'Final exam', 'Coursework',
6286+ ... 'Coursework'],
6287+ ... ['History','Geography','History','Geography'],
6288+ ... ['January','February','March','April']])
6289+ >>> df
6290+ Grade
6291+ Final exam History January A
6292+ Geography February B
6293+ Coursework History March A
6294+ Geography April C
6295+
6296+ In the following example, we will swap the levels of the indices.
6297+ Here, we will swap the levels column-wise, but levels can be swapped row-wise
6298+ in a similar manner. Note that column-wise is the default behaviour.
6299+ By not supplying any arguments for i and j, we swap the last and second to
6300+ last indices.
6301+ >>> df.swaplevel()
6302+ Grade
6303+ Final exam January History A
6304+ February Geography B
6305+ Coursework March History A
6306+ April Geography C
6307+
6308+ By supplying one argument, we can choose which index to swap the last
6309+ index with. We can for example swap the first index with the last one as
6310+ follows.
6311+
6312+ >>> df.swaplevel(0)
6313+ Grade
6314+ January History Final exam A
6315+ February Geography Final exam B
6316+ March History Coursework A
6317+ April Geography Coursework C
6318+
6319+ We can also define explicitly which indices we want to swap by supplying values
6320+ for both i and j. Here, we for example swap the first and second indices.
6321+
6322+ >>> df.swaplevel(0,1)
6323+ Grade
6324+ History Final exam January A
6325+ Geography Final exam February B
6326+ History Coursework March A
6327+ Geography Coursework April C
6328+
62816329 """
62826330 result = self .copy ()
62836331
0 commit comments