w3resource

Pandas DataFrame: sort_values() function

DataFrame - sort_values() function

The sort_values() function is used to sort by the values along either axis.

Syntax:

DataFrame.sort_values(self, by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')

Parameters:

Name Description Type/Default Value Required / Optional
by       Name or list of names to sort by.
  • if axis is 0 or ‘index’ then by may contain index levels and/or column labels
  • if axis is 1 or ‘columns’ then by may contain column levels and/or index labels
Changed in version 0.23.0: Allow specifying index or column level names.

str or list of str Required
axis     Axis to be sorted. {0 or ‘index’, 1 or ‘columns’}
Default Value: 0
Required
ascending     Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by. bool or list of bool
Default Value: True
Required
inplace   If True, perform operation in-place. bool
Default Value: False
Required
kind   Choice of sorting algorithm. See also ndarray.np.sort for more information. mergesort is the only stable algorithm. For DataFrames, this option is only applied when sorting on a single column or label. {‘quicksort’, ‘mergesort’, ‘heapsort’}
Default Value: ‘quicksort’
Required
na_position   Puts NaNs at the beginning if first; last puts NaNs at the end. {‘first’, ‘last’}
Default Value: ‘last’
Required

Returns: sorted_obj - DataFrame or None DataFrame with sorted values if inplace=False, None otherwise.

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - pivot_table() function
Next: DataFrame - nlargest() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/dataframe/dataframe-sort_values.php