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 • 283,422 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:34:30 of on-demand video • Updated September 2020

  • 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 panda series object. Let's begin by re-importing osseous Vee's 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 Pokemon. We do a dot after it. And let's start right in 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 0 1 2 and 3. And now we shifted that out of order because we sorted 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 called dot head after Scott 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 together. 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 suburb though so let's just do poker hand on 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 there 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 Pokemon 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 rows 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 Pokemon 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. 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 loops 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 sort. 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 1 and thats going to give us the greatest stock price. So Google's highest stock price at least among this data set was seven hundred eighty two dollars and 22 cents. So that's the sort values method as well as the sending parameter that's available within it and 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 sort of 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 the entire thing. We can see here that the index is still in its original default order which means the values 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 we'll 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.