Skip to content

Commit 4a20781

Browse files
DOC add example to reorder levels (#42935)
* Update frame.py Response to changes suggested on my previous pull request * Update frame.py Fixed the indentation issue * Update frame.py Built the example inside the docstring and removed the unnecessary inplace=true * fixup Co-authored-by: Marco Gorelli <[email protected]>
1 parent 5600a2f commit 4a20781

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pandas/core/frame.py

+25
Original file line numberDiff line numberDiff line change
@@ -6837,6 +6837,31 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame:
68376837
Returns
68386838
-------
68396839
DataFrame
6840+
6841+
Examples
6842+
--------
6843+
>>> data = {
6844+
... "class": ["Mammals", "Mammals", "Reptiles"],
6845+
... "diet": ["Omnivore", "Carnivore", "Carnivore"],
6846+
... "species": ["Humans", "Dogs", "Snakes"],
6847+
... }
6848+
>>> df = pd.DataFrame(data, columns=["class", "diet", "species"])
6849+
>>> df = df.set_index(["class", "diet"])
6850+
>>> df
6851+
species
6852+
class diet
6853+
Mammals Omnivore Humans
6854+
Carnivore Dogs
6855+
Reptiles Carnivore Snakes
6856+
6857+
Let's reorder the levels of the index:
6858+
6859+
>>> df.reorder_levels(["diet", "class"])
6860+
species
6861+
diet class
6862+
Omnivore Mammals Humans
6863+
Carnivore Mammals Dogs
6864+
Reptiles Snakes
68406865
"""
68416866
axis = self._get_axis_number(axis)
68426867
if not isinstance(self._get_axis(axis), MultiIndex): # pragma: no cover

0 commit comments

Comments
 (0)