 Hello, everybody. Today we're going to be looking at filtering and ordering data frames in pandas. There are a lot of different ways you can filter and order your data in pandas. And I'm going to try to show you all of the main ways that you can do that. So let's kick it off by importing our data set. So we're going to say data frame is equal to and we'll say pandas, and I need to import my pandas. So we'll say import andas as pd. That's pretty important, I think. So pd.read underscore csv and we'll do r and then we'll say the world population csv. So let's run this. All our data frame right here. And this is the data frame that we're going to be filtering through and ordering in pandas. So let's kick it off. The first thing that we can do is filter based off of the columns. So the data within our columns, so Asia, Europe, Africa, or whatever data we may have in that column. Let's go right down here. We're going to say df and then within it, we're going to specify what column we're going to be filtering on. So we're going to say df with another bracket and we'll say rank. So we're going to be looking at this rank column right here. And then we'll say in that rank column, we want to do greater than 10. And that's actually going to be a lot of them. Let's do less than. So when we run this, it's only going to return these values that are less than 10. We can also do less than or equal to, you know, all of these comparison operators. So less than or equal to. So now we have all of the ranks one through 10. Now, if we look at these countries, we can specify by specific values almost exactly like we did here. But instead of doing a comparison operator like we did right here, and including those names, let's say Bangladesh and Brazil, we can use the is in function, almost like an in function in SQL, if you know SQL. So let's go right down here. And we're going to say specific underscore countries. So right now we're just going to make a list of the countries that we want. And then we'll say Bangladesh and Brazil. So let's go right down here. And we'll say, okay, for these specific countries, from the data frame, let's do our bracket, we'll say in this country column. So we'll do data frame, and then another bracket for country. So in this country column, we can do dot is in, and then open parentheses, and then look for our specific countries. So we're looking at just this column. And we're saying is in. So we're looking at are these values within this column. And we're getting this error. And this looks very, very odd. Let me um, this doesn't look right. There we go. I just had some syntax errors, I apologize, made it way more complicated than it needs to be. But here's how you use this is in function. So we're looking at Bangladesh and Brazil. And we return those rows with Bangladesh and Brazil really quickly. I wanted to give a huge shout out to the sponsor of this entire panda series. And that is you to me, you to me has some of the best courses at the best prices. And it is no exception when it comes to pandas courses. If you want to master pandas, this is the course that I would recommend. It's going to teach you just about everything you need to know about pandas. So huge shout out to you to me for sponsoring this panda series. And let's get back to the video. We can also do a contains function, kind of similar to is in, except it's more like the like in the sequel as well. I'm comparing a lot of this to sequel, because when you're filtering things, I always, my brain always goes to sequel. But in pandas, it's called the contains. So let's do, let's actually copy this, because I don't want to make the same mistake again. Let's do that. And we'll do the bracket. But instead of dot is in, we're gonna do dot string dot contains, and then an open parentheses. So we're going to be looking for a string. If it contains, if it contains, let's do United, almost like United States or any other United. So let's run this. And as you can see, we have United Arab Emirates, United Kingdom, United States, United States, Virgin Islands. So we can kind of search for a specific string or a number or a value within our data or within that column of country. Now, so far, we've only been looking at how you can filter on these columns. We can also filter based off of the index as well. And there's two different ways you can do it or two of the main ways. There's filter. And then there's a look and I look, look stands for location and I look stands for integer location. And if you've seen other previous videos, I've kind of mentioned those. So we can take a quick look at all of those. So really quickly, we need to set an index because the index right now is not the best. We'll set our index to country. So let's say DF two is equal to DF dot set underscore index. And we'll say country. I'm just doing DF two, because later on, I want to use that data frame again. So I'm just going to assign it to another data frame so that we can just easily switch back and forth. So now we have this index as the country. And what we can do is use the filter function. So let's go down here, we'll say DF two dot filter. We'll do an open parentheses. And now we can specify our items. So these are actually going to be specifying which columns we want to keep. So we're gonna say items is equal to then we'll make a list. We'll say continent. Hope that's how we spell continent. I'm always messing up with my, my stuff here, my spelling. Then we'll do CCA three, because why not, you can specify whichever ones you want. When we run this, it's going to only bring in those two columns. Now by default, it's choosing the access for us. But we can also specify which axis we want to search on. So if we say access is equal to zero, it's actually going to search this axis. This is the zero axis. This is the one axis. So where our columns are is one. So if we go back and do one, we're searching on that one axis or those header accesses again. And this is the default, but you can specify that. So if you just want to search on, you know, filtering right here, you can do that. And let's actually copy this and do that right down here. Just so you can see what it looks like. But let's search for Zimbabwe and we'll do Zimbabwe and we'll be looking at the zero axis, which is the up and down on the left hand side. And when we filter on that, we can filter by Zimbabwe by looking just at the country index. We can also use the like just like we did before. And I'll show you the exact same demonstration that we did, which you can say like is equal to, and instead of having to put in a concrete text, you can just say United, just like we did before. And we're searching where the axis is equal to zero, which again is this left handed access. So now we're looking for United. And it's going to give us all of the countries or all the indexed values that have United in it. Like we were talking about before, we also have Locke and iLocke. So we can say DataFrame2.Locke. Now, this is a specific value. So we'll do United States. So location is just looking at the actual name or the value of it, not its position. So if we search for United States, it's going to give us this right here, where it gives us all of the columns for United States, and then all of the values for United States. Or we can do the iLocke, which is the integer location, which is not the exact same, because we're looking at the string for the Locke. We're looking at this string, but underneath it, there still is a position. That's that integer location. Let's do a completely random one. Let's just say three. If we look at the third position, it's going to give us ASM, which I'm not exactly sure what it is, but it still gives us basically the same kind of output, which is the columns and the values. So that's another way that you can search within your index when you're actually trying to filter down that data. Now, let's go look at the order buy. And let's start with the very first one that we looked at. Let's do DataFrame. That's why I kept it because I wanted to use it later. Now we can sort and order these values instead of it just being kind of a jumbled mess in here. We can sort these columns however we would like ascending, descending, multiple columns, single columns, and let's look at how to do that. So we'll say DataFrame and then we'll do DataFrame, look at rank again, just like we were doing above. And let's do DataFrame where it's less than 10. I should have just gone and copied this. I apologize. So now we have this DataFrame that is greater than 10. Now we can do dot sort underscore values. And this is the function that's going to allow us to sort everything that we want to sort. So we can do buy is equal to and we'll just order it by the exact same thing that we were doing or calling it on. We'll do rank. So now what this is going to do is going to order our rank column. And as you can see, did that 12345, we can also do it with ascending or descending. So if you want to, you can look in here and see what you can do. So we'll do ascending. We'll say that's equal to true. And so that's the automatic default. So that didn't change anything. But if we say false, it's going to be descending from highest to lowest. So now we have it in the opposite direction. Now we don't have to just order or sort this on one single column, we can do multiple columns. And we can do that by making a list right here. Whoops, make a list. Just like that. And we'll input different ones as well. So now let's input our country. And when we run this, it will give us rank of nine, eight, seven, six, as well as the country of Russia, Bangladesh, Brazil. Now, if you noticed the country really didn't change because the rank stayed the exact same. That's because there's an order of importance here. And it starts with the very first one. If we change this around and we look at this one and put it on right here, now the country is going to be descended and the rank would come second. So it's not going, the rank isn't going to really have any effect here. So now we have the country, United States, Russia, Pakistan, and the rank really didn't get ordered at all. Now if we want to see how that can actually work, let's do continent right here and actually put it right here and do country here. So if we run this, it's first going to come and it's going to organize or sort the continent. Then it's going to come back and go to the country and then it's going to sort the country. So keep your eye right here in this Asia area because we're going to sort this differently than ascending. So we have ascending false and that applies to both of these. It's false and false. But we can specify which one we want to do. We can do a false here and a true here. So we'll do false comma true. And what this is going to do is it's going to say false for the continent. So the continent right here is going to stay the exact same. And so that is a lot of how you can filter and order your data within pandas. I hope that this was helpful. I hope that you enjoyed this video. If you liked it, be sure to like and subscribe below. Check out all my other videos on Python and pandas and I will see you in the next video.