bpd.DataFrame.sort_values

DataFrame.sort_values(by, *, ascending=True)[source]

Sort by the values in column(s) named in by.

Parameters:
  • by (str or list of str) – Name or list of column names to sort by.
  • ascending ({True, False} or list of bool, keyword only) – Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by. Default is True.
Returns:

sorted_obj

Return type:

DataFrame

Raises:

KeyError – If by not found in columns.

Examples

>>> df = bpd.DataFrame().assign(name=['Sally', 'George', 'Bill', 'Ann'],
...                             age=[21, 25, 18, 28],
...                             height_cm=[161, 168, 171, 149])
>>> df.sort_values(by='age')
     name  age  height_cm
2    Bill   18        171
0   Sally   21        161
1  George   25        168
3     Ann   28        149
>>> df.sort_values(by='height_cm', ascending=False)
     name  age  height_cm
2    Bill   18        171
1  George   25        168
0   Sally   21        161
3     Ann   28        149