@@ -6278,6 +6278,52 @@ def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
6278
6278
Returns
6279
6279
-------
6280
6280
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 last indices.
6300
+ >>> df.swaplevel()
6301
+ Grade
6302
+ Final exam January History A
6303
+ February Geography B
6304
+ Coursework March History A
6305
+ April Geography C
6306
+
6307
+ By supplying one argument, we can choose which index to swap the last index with.
6308
+ We can for example swap the first index with the last one as follows.
6309
+
6310
+ >>> df.swaplevel(0)
6311
+ Grade
6312
+ January History Final exam A
6313
+ February Geography Final exam B
6314
+ March History Coursework A
6315
+ April Geography Coursework C
6316
+
6317
+ We can also define explicitly which indices we want to swap by supplying values
6318
+ for both i and j. Here, we for example swap the first and second indices.
6319
+
6320
+ >>> df.swaplevel(0,1)
6321
+ Grade
6322
+ History Final exam January A
6323
+ Geography Final exam February B
6324
+ History Coursework March A
6325
+ Geography Coursework April C
6326
+
6281
6327
"""
6282
6328
result = self .copy ()
6283
6329
0 commit comments