Skip to content

DOC added example to reorder levels #43134

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 3 commits 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
26 changes: 26 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6837,6 +6837,32 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame:
Returns
-------
DataFrame

Examples
--------
>>> data={'Name':['Rahul', 'Arijit', 'Tushar'],
... 'Mark':[70,95,89],
... 'Branch':['CSE', 'EEE', 'IT'],
... 'Grade':['C', 'B', 'A'],
... }
>>> df=pd.DataFrame(data, columns = ['Mark', 'Branch', 'Grade', 'Name'])
>>> df.set_index(['Branch', 'Mark', 'Grade'], inplace = True)
Copy link
Member

Choose a reason for hiding this comment

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

also, can you please write this as df = df.set_index(['Branch', 'Mark', 'Grade'])?

Copy link
Author

Choose a reason for hiding this comment

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

Ok I will do the changes.

>>> df

Name
Branch Mark Grade
CSE 70 C Rahul
EEE 95 B Arijit
IT 89 A Tushar

>>> df.reorder_levels(['Mark', 'Grade', 'Branch'])

Name
Mark Grade Branch
70 C CSE Rahul
95 B EEE Arijit
89 A IT Tushar

"""
axis = self._get_axis_number(axis)
if not isinstance(self._get_axis(axis), MultiIndex): # pragma: no cover
Expand Down