search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas DataFrame | sort_index method

Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Sorting and Restructuring DataFrames
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas' DataFrame.sort_index(~) method sorts the source DataFrame by either column or index labels.

Parameters

1. axislink | string or int | optional

Whether to sort by index or column labels:

Axis

Description

DataFrame will be sorted by index labels.

0 or "index"

DataFrame will be sorted by column labels.

1 or "columns"

By default, axis=0.

2. level | int or string or list<int> or list<string> | optional

The level to sort by. This is only relevant if your DataFrame has multi-index.

3. ascendinglink | boolean or list<boolean> | optional

Whether to sort in ascending or descending order. By default, ascending=True.

4. inplace | boolean | optional

  • If True, then the source DataFrame will be directly modified, and no new DataFrame will be created.

  • If False, then a new DataFrame will be returned, and the source DataFrame will be kept intact.

By default, inplace=False.

5. kind | string | optional

The sorting algorithm to use:

Kind

Speed

Worst case

Memory

Stable

quicksort

1 (fast)

O(n^2)

0

no

mergesort

2

O(nlogn)

~n/2

yes

heapsort

3 (slow)

O(nlogn)

0

no

By default, kind="quicksort".

NOTE

Sorting algorithms that are "stable" retain the relative ordering of duplicate values. For instance, suppose you are sorting the array [(2,3), (2,1), (4,5)] by the first element of each tuple. We have a duplicate value of 2 here, and stable sorting algorithms ensure that (2,3) will always come before (2,1) since that is how they are ordered originally. Unstable searches provide no guarantee that such ordering is retained.

6. na_positionlink | string | optional

Where to place NaN values:

Value

Description

"first"

Place NaNs at the beginning.

"last"

Place NaNs at the end.

By default, na_position="last".

7. sort_remaining | boolean | optional

If True, then we further sort by the other inner-levels in order. This is only relevant for multi-level indexes. By default, sort_remaining=True.

8. ignore_indexlink | boolean | optional

  • If True, then the index of the sorted DataFrame will be 0,1,...,n-1, where n is the number of rows of the DataFrame.

  • If False, then the index names will be kept as is.

By default, ignore_index=False.

Return Value

A DataFrame sorted by either column or index labels.

Examples

Consider the following DataFrame:

df = pd.DataFrame({"B":[1,2,3],"C":[4,5,6],"A":[7,8,9]}, index=[2,0,1])
df
   B  C  A
2  1  4  7
0  2  5  8
1  3  6  9

Sorting by index labels

To sort by index labels:

df.sort_index()
   B  C  A
0  2  5  8
1  3  6  9
2  1  4  7

Sorting by column labels

To sort by column labels, set axis=1, like so:

df.sort_index(axis=1)
   A  B  C
2  7  1  4
0  8  2  5
1  9  3  6

Here's our df again for your reference:

df
   B  C  A
2  1  4  7
0  2  5  8
1  3  6  9

Sorting in descending order

By default, the labels are sorted in ascending order. To sort in descending order instead, set ascending=False:

df.sort_index(ascending=False)
   B  C  A
2  1  4  7
1  3  6  9
0  2  5  8

Specifying na_position

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2,3]}, index=[2,1,np.NaN])
df
     A
2.0  1
1.0  2
NaN  3

By default, na_position="last", which means that rows (axis=0) where index is NaN are placed at the end:

df.sort_index()   # na_position="last"
     A
1.0  2
2.0  1
NaN  3

To place rows where index is NaN in the beginning instead:

df.sort_index(na_position="first"
     A
NaN  3
1.0  2
2.0  1

Specifying ignore_index

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2,3]}, index=["b","a","c"])
df
   A
b  1
a  2
c  3

By default, ignore_index=False, which means that the index names are kept:

df.sort_index()
   A
a  2
b  1
c  3

By setting ignore_index=True, the index names will be reset to the default integer indices:

df.sort_index(ignore_index=True)
   A
0  2
1  1
2  3
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!