Sorting#
pandas
includes a few different built-in sorting operations. We can sort by an index for either axis of our DataFrame
(i.e. we can sort based on row index labels or by column name).
The default for .sort_values
is to sort in ascending order. We can use ascending=False
to sort in descending order.
df.sort_values('AGEP') # ascending sort for AGEP column values
df.sort_values('MAR', ascending=False) # descending sort for MAR column vlaues
NOTE: When sorting, we are returning a sorted DataFrame
. We ARE NOT updating the DataFrame
in place. We have a couple of options to sort in place.
dfSort = df.sort_values('AGEP') # create new dataframe from sort
dfSort # show output
df.sort_values('MAR', inplace=True) # store sort output in-place
df # show output
We can also sort by multiple fields.
df.sort_values(['MAR', 'AGEP'], ascending=False) # descending sort for MAR and PWGTP columns
When sorting by fields with string data, a-z
is considered ascending
and z-a
would be descending
.
Additional Resources#
For more on sorting operations in pandas
, check out the package’s “Sorting” documentation.