Day-11 Pandas Power: Filter & Order Your Python Data Like a Boss!

Akansha Yadav walks through filtering and sorting a population dataset with pandas. Learn boolean indexing, `isin`, `str.contains`, `setindex`, `.filter`, `.loc`, and `sortvalues` to organize 234 rows of data by continent and country.
Chapters
Ladies and gentlemen, hello everyone, welcome back to my YouTube channel. In today's video, we are going to talk about how to filter your data. So in Excel, we can easily filter it out using the Filter tab, but now I am going to tell you how you can do it using Pandas. I'm— we are going to learn about two concepts. One is filtering, another one is altering. So let's get back on the screen and see. So what I have done today is I have changed the dataset. Now I'm using the world population dataset which I have over here. Let me show you the dataset. So this is how the data looks like, something and we have multiple rows and on and around 235 column— 235 rows and multiple columns. So let's see how to do the filtering and ordering. So first I have imported pandas, then I have imported the data. This is our data which I have printed using df. We have 234 rows and 17 columns in total. So let's see how to filter it out. So there is one column called rank where we have the ranks 36 and so on. I want to filter out the rank column by some condition and the criteria that we did in the last class. So I have written df, then df_rank, then greater than, uh, less than is equal to 10. It means whichever rank is less than or equal to 10 will print. So here we got the output. We have the ranks where we have the filter applied. We have used equal to, that is why we are getting 10 rows. So now let's see, we have the specific country which is Bangladesh and Brazil. We have one country column over here. Now what I want is I want to see whether Brazil and Bangladesh is in the data or not. So these are some conditions that come up that I, I just, you know, created by myself that can appear while you are doing the data manipulation. So I have done data, and out of data I am taking the country column because I wanted to check whether these specific countries are there or not. If these are there in the data, then print it out. So I'm using isin, isin df_country.isin specific countries. It means Bangladesh and Brazil will come up over here. We can see we have Bangladesh and Brazil in the output. Same way, what I have done is in the next time, what I have done is df, df_country.string.contains United. I wanted to check whichever country's name consists of word United.. It may be United States or United Arab Emirates, and multiple names are there with the term United. That is what I wanted to check, which are the names present in my data with the term United. So you can see over here in the country column, wherever they found that United one is present, it printed all the rows with that particular data. Now I am trying to reset my index with the one of my existing column. Last time we did changed our like indexes with some dates or something similar to that. But now what I'm trying to do is I'm trying to reset my indexes with the existing column, that is country. So I have applied .set_index function, and the brackets I have written con— country, and it got replaced with the country. So earlier we were having 17 columns, if you have observed over here. Once I have changed indexes to country, we have 16 columns. So it means it got updated. Another is I wanted to filter out where the items are continent. I just want two things, continent and this column CCA3. This is world population data, so according to that I have taken two column continents and this. So I don't want the entire data to be printed, I just want these two columns to be printed. So for that what I have done is I have done axis is equal to 0. If I have done axis is equal to, uh, I, I'm doing axis is equal to 1. If I have done axis is equal to 0, in that case it have taken the rows. But now I'm talking about the column because I, I'm considering these items as a column and which are actually the column. That is why I have written axis is equal to 1. And here we got the output where we have the entire data with the two columns only. We have not created a subset, we have not extracted those two but just I have filtered it out. So I'm using .filter method, then applying the items to write down the column names and writing out which one I do prefer. I am preferring columns, that is why I have written axis is equal to 1. Now next, I— how I am filtering it out is I have written df2, which is my variable, .filter items is equal to Zimbabwe, and then axis is equal to 0. This time we were writing axis equal to 1 and we got the column data. Now I have written x is 0, it means this will take the country where this particular, uh, word is present. It means the country name is present and printed all the data alongside the row. This time we were getting alongside the columns. So that's how you can filter according to the row and the columns. If you are referring to the row, then go with x is equal to 0. If you are referring to the column, do x is equal to 1 and write down the items. Now what I have done is I have done this earlier. What we were doing is over here, let me take you back. Okay, so here I was checking whether the data which is, uh, df and the data consists of a country column does consist of United or not. This is what I was checking, was checking from— we were checking it from the .strcontains, but now I'm doing the same thing over here in this command saying .filter like United, axis is equal to 0, because we have already replaced the indexes to the country. Now we can easily check like United, and it printed all the data for us. Now I wanted to check, I wanted to locate where United States is in my entire data, where in United States. To locate it, I am using loc, which we have already learned sometime, but not in detail. But yes, we have learned it while we were doing the data uh, filterings. I think in, let's say, in the day 5 and 6 we did this. So LOC we have done, United States I am trying to locate. So it gave me the data related to the United States. We have the rank, CAC, CCA3, capital, continent, and population number. That's how you can filter it. Now what I am trying to do is I am trying index-based locating using bracket. Like, uh, in the bracket I have written 3. So So I will show you the original data. Yeah, if you have this, if you can see over here that this is 36, 138, 34, and 213. 0, 1, 2, 3. If we go by indexing, then this is the third number. So we should get America, Samoa, Samoa, and let me show you where it is. United States. Yeah, so when I have written 3, we are getting rank 213. Then this is the CCA 3. And we are not getting the indexes because of the— over here you can see the name as well, but we are getting that it is as an indexes. So it means we are writing the indexes and we are getting the output. Another way to filter out your data is I am doing data, then out of data I am taking rank. I am checking whichever, wherever the rank is less than 10, go with that. Then sort the values by continent and country. It will take to as a primary, which is continent and country, and then do the ascending is equal to, uh, false comma true. So let me show you what happened exactly. We have done continent and country where the ranks are less than 10. So you can see over here we have the ranks less than 10, we have the continent and we have the country. We did it back, and here we are with the output. So that's how you can do the filtering and ordering in pandas. If you like this video, don't forget to hit the like button and subscribe to my channel if you like it.










