Skip to content

Update frame.py #42667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6788,7 +6788,37 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame:
Returns
-------
DataFrame
"""

Examples
--------
>>> data={'class':['Mammals','Mammals','Reptiles','Reptiles','Amphibians','Amphibians'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think this is pep8 formatted, can you add spaces where needed

'diet':['Omnivore','Carnivore','Carnivore','Herbivore','Omnivore','Herbivore'],
'species':['Humans','Dogs','Snakes','Iguanas','Frogs','Aquatic Salamanders']
}

>>> df=pd.DataFrame(data,columns=['class','diet','species'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space around the equals


>>> df.set_index(['class','diet'],drop=True,inplace=True)

>>> df.reorder_levels(["diet", "class"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this how it actually renders?

diet class species
Omnivore Mammals Humans
Carnivore Mammals Dogs
Reptiles Snakes
Herbivore Reptiles Iguanas
Omnivore Amphibians Frogs
Herbivore Amphibians Aquatic Salamanders

>>> df.reorder_levels(["class","diet"])
class diet species
Mammals Omnivore Humans
Carnivore Dogs
Reptiles Carnivore Snakes
Herbivore Iguanas
Amphibians Omnivore Frogs
Herbivore Aquatic Salamanders
"""

axis = self._get_axis_number(axis)
if not isinstance(self._get_axis(axis), MultiIndex): # pragma: no cover
raise TypeError("Can only reorder levels on a hierarchical axis.")
Expand Down