Pandas DataFrame | sort_index method
Start your free 7-days trial now!
Pandas' DataFrame.sort_index(~)
method sorts the source DataFrame by either column or index labels.
Parameters
1. axis
link | string
or int
| optional
Whether to sort by index or column labels:
Axis | Description |
---|---|
| DataFrame will be sorted by index labels. |
| DataFrame will be sorted by column labels. |
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. ascending
link | 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) |
| 0 | no |
mergesort | 2 |
| ~n/2 | yes |
heapsort | 3 (slow) |
| 0 | no |
By default, kind="quicksort"
.
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_position
link | string
| optional
Where to place NaN
values:
Value | Description |
---|---|
| Place |
| Place |
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_index
link | boolean
| optional
If
True
, then the index of the sorted DataFrame will be0,1,...,n-1
, wheren
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
B C A2 1 4 70 2 5 81 3 6 9
Sorting by index labels
To sort by index labels:
df.sort_index()
B C A0 2 5 81 3 6 92 1 4 7
Sorting by column labels
To sort by column labels, set axis=1
, like so:
df.sort_index(axis=1)
A B C2 7 1 40 8 2 51 9 3 6
Here's our df
again for your reference:
df
B C A2 1 4 70 2 5 81 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 A2 1 4 71 3 6 90 2 5 8
Specifying na_position
Consider the following DataFrame:
df
A2.0 11.0 2NaN 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"
A1.0 22.0 1NaN 3
To place rows where index is NaN
in the beginning instead:
df.sort_index(na_position="first")
ANaN 31.0 22.0 1
Specifying ignore_index
Consider the following DataFrame:
df
Ab 1a 2c 3
By default, ignore_index=False
, which means that the index names are kept:
df.sort_index()
Aa 2b 1c 3
By setting ignore_index=True
, the index names will be reset to the default integer indices:
df.sort_index(ignore_index=True)
A0 21 12 3