How To Sort DataFrame Based On Index?


How To Sort DataFrame Based On Index?

Table Of Contents:

  1. Syntax ‘sort_index( )’ Method In Pandas.
  2. Examples ‘sort_index( )’ Method.

(1) Syntax:

DataFrame.sort_index(*, axis=0, level=None, ascending=True, inplace=False, 
                    kind='quicksort', na_position='last', sort_remaining=True, 
                    ignore_index=False, key=None)

Description:

  • Sort object by labels (along an axis).

  • Returns a new DataFrame sorted by label if inplace argument is False, otherwise updates the original DataFrame and returns None.

Parameters:

  • axis: {0 or ‘index’, 1 or ‘columns’}, default 0 –
    • The axis along which to sort. The value 0 identifies the rows, and 1 identifies the columns.
  • level: int or level name or list of ints or list of level names –
    • If not None, sort on values in specified index level(s).
  • ascending: bool or list-like of bools, default True –
    • Sort ascending vs. descending. When the index is a MultiIndex the sort direction can be controlled for each level individually.
  • inplace: bool, default False –
    • Whether to modify the DataFrame rather than create a new one.
  • kind: {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, default ‘quicksort’ –
    • Choice of sorting algorithm. See also numpy.sort() for more information. mergesort and stable are the only stable algorithms. For DataFrames, this option is only applied when sorting on a single column or label.
  • na_position{‘first’, ‘last’}, default ‘last’ –
    • Puts NaNs at the beginning if firstlast puts NaNs at the end. Not implemented for MultiIndex.
  • sort_remaining: bool, default True –
    • If True and sorting by level and index is multilevel, sort by other levels too (in order) after sorting by specified level.
  • ignore_index: bool, default False –
    • If True, the resulting axis will be labeled 0, 1, …, n – 1.
  • key: callable, optional –
    • If not None, apply the key function to the index values before sorting. This is similar to the key argument in the builtin sorted() function, with the notable difference that this key function should be vectorized. It should expect an Index and return an Index of the same shape. For MultiIndex inputs, the key is applied per level.

Returns:

  • DataFrame or None –
    • The original DataFrame sorted by the labels or None if inplace=True.

(2) Examples Of sort_index() Method:

Example-1:

df = pd.DataFrame([1, 2, 3, 4, 5], index=[100, 29, 234, 1, 150],
                  columns=['A'])
df

Output:

# Sorting Based On Index

df.sort_index()

Output:

# By default, it sorts in ascending order, to sort in descending order, use ascending=False

df.sort_index(ascending=False)

Output:

# A key function can be specified which is applied to the index before sorting. # For a MultiIndex this is applied to each level separately.

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

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *