Swap levels i and j in a :class:`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. copy : bool, default Fals
(
self, i: Level = -2, j: Level = -1, copy: bool | lib.NoDefault = lib.no_default
)
| 4229 | return selectn.SelectNSeries(self, n=n, keep=keep).nsmallest() |
| 4230 | |
| 4231 | def swaplevel( |
| 4232 | self, i: Level = -2, j: Level = -1, copy: bool | lib.NoDefault = lib.no_default |
| 4233 | ) -> Series: |
| 4234 | """ |
| 4235 | Swap levels i and j in a :class:`MultiIndex`. |
| 4236 | |
| 4237 | Default is to swap the two innermost levels of the index. |
| 4238 | |
| 4239 | Parameters |
| 4240 | ---------- |
| 4241 | i, j : int or str |
| 4242 | Levels of the indices to be swapped. Can pass level name as string. |
| 4243 | copy : bool, default False |
| 4244 | This keyword is now ignored; changing its value will have no |
| 4245 | impact on the method. |
| 4246 | |
| 4247 | .. deprecated:: 3.0.0 |
| 4248 | |
| 4249 | This keyword is ignored and will be removed in pandas 4.0. Since |
| 4250 | pandas 3.0, this method always returns a new object using a lazy |
| 4251 | copy mechanism that defers copies until necessary |
| 4252 | (Copy-on-Write). See the `user guide on Copy-on-Write |
| 4253 | <https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html>`__ |
| 4254 | for more details. |
| 4255 | |
| 4256 | Returns |
| 4257 | ------- |
| 4258 | Series |
| 4259 | Series with levels swapped in MultiIndex. |
| 4260 | |
| 4261 | See Also |
| 4262 | -------- |
| 4263 | DataFrame.swaplevel : Swap levels i and j in a :class:`DataFrame`. |
| 4264 | Series.reorder_levels : Rearrange index levels using input order. |
| 4265 | MultiIndex.swaplevel : Swap levels i and j in a :class:`MultiIndex`. |
| 4266 | |
| 4267 | Examples |
| 4268 | -------- |
| 4269 | >>> s = pd.Series( |
| 4270 | ... ["A", "B", "A", "C"], |
| 4271 | ... index=[ |
| 4272 | ... ["Final exam", "Final exam", "Coursework", "Coursework"], |
| 4273 | ... ["History", "Geography", "History", "Geography"], |
| 4274 | ... ["January", "February", "March", "April"], |
| 4275 | ... ], |
| 4276 | ... ) |
| 4277 | >>> s |
| 4278 | Final exam History January A |
| 4279 | Geography February B |
| 4280 | Coursework History March A |
| 4281 | Geography April C |
| 4282 | dtype: str |
| 4283 | |
| 4284 | In the following example, we will swap the levels of the indices. |
| 4285 | Here, we will swap the levels column-wise, but levels can be swapped row-wise |
| 4286 | in a similar manner. Note that column-wise is the default behavior. |
| 4287 | By not supplying any arguments for i and j, we swap the last and second to |
| 4288 | last indices. |