O'Reilly logo

Hands-On Data Analysis with NumPy and pandas by Curtis Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sorting by values

If we wish to sort the rows of a DataFrame or the elements of a series, we need to use the sort_values method. For a series, you'd call sort_values and call it a day. For a DataFrame though, you would need to set the by parameter; you can set by to a string, indicating the column you want to sort by, or to a list of strings, indicating column names. Sorting will first proceed according to the first column in this list; then, when ties appear, sorting will be according to the next column, and so on.

So, let's demonstrate some of these sorting techniques. We sort the values of the DataFrame according to the column AAA, shown in the following screenshot:

Notice that all the entries in AAA are now in order, though not much ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required