Use the sort_values method to sort a Series in ascending or descending order

Boris Paskhaver
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
4.7 instructor rating • 6 courses • 313,060 students

Lecture description

Call the .sort_values() method on a Series to sort the values in ascending or descending order. We'll see how this command operates on both a numeric and alphabetical dataset.

Learn more from the full course

Data Analysis with Pandas and Python

Analyze data quickly and easily with Python's powerful pandas library! All datasets included --- beginners welcome!

20:33:02 of on-demand video • Updated November 2021

  • Perform a multitude of data operations in Python's popular "pandas" library including grouping, pivoting, joining and more!
  • Learn hundreds of methods and attributes across numerous pandas objects
  • Possess a strong understanding of manipulating 1D, 2D, and 3D data sets
  • Resolve common issues in broken or incomplete data sets
English [Auto] All right, in this lesson, we'll explore the sort values method, which is available on any Pandas series object. Let's begin by reimporting our ourselves and let's just dive right into it by calling these sort values method and then we'll explore it in more detail in just a second. So we have our series, which is Pokémon. We do a dot after it and let's start writing the name of our method sort. Let's press our tab key here. Here we see it below. It has an underscore in the middle sort. Underscore values. Let's select it with enter all methods require a parentheses at the end and let's execute. And what this is going to give us when we're dealing with a series of strings is basically a brand new series where the values are going to be in alphabetical order. So you can see the index to the left has been all messed up. It began with the default numeric scheme of zero one, then two, then three. And now we've shifted that out of order because we've started the series by its values alphabetically. Now, the important thing to take away here, and I briefly mentioned this in a previous lesson, is that this is returning a brand new series object. And because it's a brand new series and it comes bundled with its own set of methods and attributes, we can actually continue calling new methods and new attributes on it. This is something that's called method chaining because it creates a chain of sequential methods. So, for example, this resulting series is a brand new series. So it's going to have a head method available on it. Head, as we know, returns a new series with just the top five rows by default. So if we call dot head, AFTRS dot sort values on the original Pokemon set, it's going to first calculate this series, which is alphabetically sequenced. And then the head method is just going to give us the first five rows of that series as a brand new series. So that's just a little bit on method chaining. And this is an operation that we're going to be using a lot throughout these lessons, usually to save time. So let's take another look at this. Let's move on to a Cibolo. So let's just do Pokémon that sort values again. And this time within my parentheses, I want to take a look at the available parameters for this method. So I'm going to hold my shift key and press tab and then we can see some of the available options. Now, the one I want to focus on, particularly in this lesson, is ascending, which by default is set to true. So executing it without any custom arguments is basically the exact same thing as writing. Ascending equals true because that's already the default argument. The result will be the exact same because that's the standard. However, if we want to sort it in reverse alphabetical order or descending, we have to modify the argument that we give to the ascending parameter. And predictably, instead of true, we're going to want to set it to false when we provide the boolean argument false to the ascending parameter of the sort values method. We're going to get a new series that sorts the existing Pokémon series Values in reverse alphabetical order. And just as always, we can call yet another method on top of this, because this is another series. So if we want, let's say, the last five arrows here, which will also be the same results as the one above, because it's in reverse order, we can just call the tail method with parentheses. And there are the last five rows of our Pokémon series sorted in reverse alphabetical order. And you can see that they match the way that they are up here. They're just flipped around due to the way that we sorted it. So let's try the sort values method again, but this time on a numeric series. So we have Google, which, as you recall, is a series of floating points so predictably by itself sort values. And that's the same thing as giving it the argument of true to ascending because that's the default. So that's what it's going to use even if we don't specify it. But that's going to sort the series in ascending order, which means the values are going to start at the smallest value and proceed upwards to the greatest value. So this is equivalent to writing. Oops, equivalent to writing this same thing. No difference between the series, as you can see below. But if we want to instead sort of in reverse order or in descending order to have the greatest values show up at the top and the smallest values show up at the very end, we once again have to flip around the argument that we provide to the ascending parameter. So if we set it to false, that makes it a descending sword. And once again, this is returning a brand new series. So this series has its own attributes and its own methods available. So if we want to see the greatest stock price that Google ever had, we can just take the top row of this Sorted Values series with head one, and that's going to give us the greatest stock price. So Google's higher stock price, at least among this data set, was seven hundred eighty two dollars and twenty two cents. So that's these sort values method as well as the. Sending parameter that's available within it in a in another key takeaway that will lead us into the upcoming lesson is to recognize this whenever we've called these sort values methods on our data for our series, sorry, we have not actually modified the original series. So, for example, we can see here the first row of our Sorted Values series. And if I give you the first row of my regular Google series, you can see that it's still my original series. In fact, let me go ahead and show you the entire thing. We can see here that the index is still in its original default order, which means the values that have not been jumbled up or sorted in either ascending or descending order. It's just the way that it was important. And so these sort values method by itself does not modify the existing series. It just returns a brand new one and in the next lesson will explore how to make operations like sort values permanent. In other words, overwrite the existing series and we'll be using something called the in-place parameter option. So I'll see you in the next lesson.