How To Sort Pandas DataFrame ?
Table Of Contents:
- Syntax ‘sort_values( )’ Method In Pandas.
- Examples ‘sort_values( )’ Method.
(1) Syntax:
DataFrame.sort_values(by, *, axis=0, ascending=True, inplace=False, kind='quicksort',
na_position='last', ignore_index=False, key=None)
Description:
- Sort by the values along either axis.
Parameters:
- by: str or list of str –
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.
- axis: {0 or ‘index’, 1 or ‘columns’}, default 0 –
- Axis to be sorted.
- ascending: bool or list of bool, default True –
- Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.
- inplace: bool, default False –
- If True, perform operation in-place.
- 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.
- Choice of sorting algorithm. See also
Returns:
- DataFrame or None –
- DataFrame with sorted values or None if
inplace=True
.
- DataFrame with sorted values or None if
(2) Examples Of sort_values( ) Method:
Example-1:
df = pd.DataFrame({
'col1': ['A', 'A', 'B', np.nan, 'D', 'C'],
'col2': [2, 1, 9, 8, 7, 4],
'col3': [0, 1, 9, 4, 2, 3],
'col4': ['a', 'B', 'c', 'D', 'e', 'F']
})
df
Output:
# Sort by col1
df.sort_values(by=['col1'])
Output:
# Sort by multiple columns
df.sort_values(by=['col1', 'col2'])
Output:
# Sort Descending
df.sort_values(by='col1', ascending=False)
Output:
# Putting NAs first
df.sort_values(by='col1', ascending=False, na_position='first')
Output:
# Sorting with a key function
df.sort_values(by='col4', key=lambda col: col.str.lower())