1. Home
  2. Explore
  3. Computer Science
  4. Pandas for Data Analysis

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Pandas for Data Analysis

Transcript

Learn how to use pandas for data analysis and data science in this full course for beginners. You will learn pandas by example from Santiago. He is an experienced developer and he also created one of the most popular data analysis courses on the internet. So you are in good hands. Let's get started. Hello, my name is Santiago and this is pandas by example, a collaboration between Free Code Camp and Data Wars. I'm originally from Argentina, world champions of the 2022 football or soccer World Cup. And why I'm telling you this? Well, as any other Argentinian, I've watched and played football pretty much my entire life. And you know what I have learned? That you don't get better at playing football just by watching games on TV. This seems obvious, right? To become a better footballer, you have to go out and practice. The same applies for data science or any other analytical discipline. Just watching videos doesn't make you an expert.

You have to practice and put your skills to a test. That's exactly what we're trying to do at Data Wars. Data Wars is a platform 100% focused on practicing data science and applying your skills with real life projects. There are no videos, just projects to resolve with bite size activities that you can check at each stage of the process. The best part is that it's entirely free and we'll explain you how to sign up in just a bit. And this is also the reasoning behind this by example series. This video focuses on resolving real projects, brainstorming and discussing the solutions and the pitfalls along the process. We'll encourage you to try to solve the projects by yourself first, even pause the video before we reveal the solutions. We want you to put your skills to a test and challenge yourself. This video covers all the most important aspects of data management with pandas, including data analysis, data cleaning and data wrangling. Each project focuses on a different topic

and the complexity grows from start to finish. We have separated each project as a chapter and you can jump back and forth. As we've mentioned before, we encourage you to pause the video and try to resolve each activity by yourself first. You can find a list of all the projects we are resolving and the instructions to sign up at datawars.io.freecodecamp. With all that said, let's just get started solving projects right now. In this project, we're going to be practicing our skills

dealing with pandas data frames. We're going to be doing some selection, understanding index selection. We're going to be creating a new column and we're going to do some statistical summarization methods. And finally, we're going to do some question answering by creating conditionals, okay? So like selection based on different conditions or queries that we want to write. So this is kind of on the simpler side for this series. We're just getting started with data frames,

again, doing statistical methods, some selection by the index, creating a column, doing some conditionals and that's it. If you feel comfortable with these topics, just move ahead to other sections of the video so we get a little bit more challenging. As usual, I'm going to encourage you to just pause the video before each activity and try to resolve it by yourself. So you can gain some of that application of the skills, right? Worst case scenario, you can just resume the video

and take a look at the way I resolved it. And by the way, there are going to be different ways of resolving these things, these activities. So you might even stumble upon something new that you do by yourself and I do something different and that can be a very rich experience. So let's get started. This data frame, the data set we're using, is a data set containing a big list of English words and the index is going to be the word itself and we're going to talk about the index in a second.

And then we have two columns, chart count or character count and the value of the word. The character count that's easy is just the length of the word, that's it. And the value is computed by adding up all the individual values of each character and the individual values are just defined in this way. So A is one, B is two, C is three, et cetera. So in this case, it's one plus one. One plus one plus the value of H, which I don't know where is H, 10, 11, whatever. Actually, we know it is eight

because this is one plus one is two and the value is 10. So H is probably the word, the character number eight. And that's it. This is already given. So I have already read the data frame and we're going to get started with some activities. The important piece about this data frame is that the index is the word, like the words themselves make the index. So whenever we want to access a given word, we can just do it by the index. And this is why it's so important to understand

how indices work in pen. So let's get started right off the bat with the first activity, which is how many elements does this data frame have? There are multiple ways of answering that. The first one is with the info method, which is one of the most common ones. Pretty quickly, we go with info and it gives us an understanding of the index. It tells us that the index, just an index with entries that go from AA, and that's it, to these words, which I don't know what it means.

And we have a total of 172,821 words. We can also use the shape method. This gives us kind of the shape of the matrix if you want in the data frame. And of course, the values are the same. So we're going to plug this one in and see how it goes. There you go, it worked. Okay, moving forward. What is the value of the word this thing that I'm not going to even pronounce? And this is a very interesting activity. Because remember, this is a very interesting activity.

Because remember, we're going to be doing selection by index. So as the index is the word itself, I can just use the dot lock method, and this should be familiar, dot lock, that does index selection. So it can just pass whatever value I want for an index using the dot lock method, and that's going to give me the results. In this case, it gives me the row itself, the entire row. So we did like this row selection, but the result is in a series. So the projection, to put it away,

the result of row selection, which is kind of horizontal structure, is going to be a vertical structure, which is a series. You see the shaping, the transposing operations. We go from something that is a row to something that is a series. And it's not pretty special. Don't pay too much attention to it. It's just to refer to the fact that the row is represented as a series. And here, we can see that the value of this word is 317.

Something also very convenient if you're doing selection is that the df.lock method, and we're going to increase here the size, df.lock accepts two parameter, the index selection and then the columns, right? Why is that? Imagine that we have 100 columns. It's going to be very hard to just browse, scroll to the value we're looking for. You can just pause here what the column we want to use. In this case, we're going to use a value right there. And the value is 317. So let's go ahead and try it.

And it worked as expected. What is the highest possible value of a word? df.we're going to use two methods. Let me show you once again, data frame. What is the highest possible value? We can do df at the value and do a max method, right? That's going to give us the maximum value there. Or we can just do df.max to have the max statistical method computed for the whole data frame, right? In this case, we see again that the value is 319. Or something that is pretty common is df.describe. And this method is just going to give you this scribe. There we go. Oh, got it completed.

It's going to give you summary statistics of all your numeric columns. In this case, we only have two columns and both are numeric, so we're good. And it gives you summary statistics of all the values right there. So for example, the average value, the average char count, the minimum value, the maximum value for character count, the maximum value for value, et cetera. It's going to give us some summary statistics. So I think we have multiple methods. We're going to go here with 319 and submit the activity and that worked.

Now the next one, which of the following words have a char count of seven and a value of 87. And this is pretty interesting because in the way we did selection before, I'm going to copy this thing right here. We, the way we did it before was just by passing one value of the index. We worked with just one word. But selection in pandas, df.lock here, takes potentially a list of index values to retrieve. So here, instead of passing just one value, I can pass each one of them.

And there's going to be a little bit of an annoying procedure. I'm going to just copy and paste all the words here. You feel free to fast forward. I will not copy this one because it definitely has more than seven characters. Just going to, you know, quickly and see which ones have seven characters. I think this one has more than seven characters but doesn't matter. It's either anyways, and now you can see that with df.lock, you can actually pass multiple indices

if you want to get multi-selection or just one, you want to select just one individual row. And to answer the activity, which of the following words have a char count of seven and a value of 87? It's glowing right here. We can just put here glowing and submits and that works. Just a common, as we were doing before, the first element in this case is a list is the list of indices to select. The second one could potentially be just a column. So here I could say value for them.

Oh, I missed a square bracket. And I just get the value for each one of these rows. Of course, in this activity, we did both the char count and the value. So that's why I didn't do it, but just a clarification. So what is the highest possible length of a word? We did it before with describe. We're going to do it again and char count. The maximum one seems to be 28, there you go. Moving on to the next section, which is exploring interesting words. So we did before this activity, there was that this word, and I'm not gonna pronounce, is the only word with a value of 317. Find the only word with a value of 319.

And there are multiple ways of solving this one. We could, let's say we do some sorting first. It's not gonna be the most effective way, but let's do some sorting. Let me see if I can put everything in the same frame. df.sortvaluesby and value, it's gonna be, I'm gonna say ascending false. In this case, as this word was the one with the highest value, just by sorting, we were able to reach that result. But of course, that is not always the case. What happens if here, it's asking us for, I don't know, the value of 18. And that is clearly, or even worse, 50. It's clearly in the middle. I have to just browse the data frame to find the correct answer. In this case, what we need to do is some selection. We need to do df at, I'm gonna do the whole syntax

and we're gonna explain it later. .lock df at value, it's gonna be 319. And this is the first idea of conditional selection in pandas. We have the word, I'm gonna just plug it in, see if it worked. And if we have a good activity, there we go, it worked. Now, how does this thing work? Remember that whenever I assume you have worked with this before, whenever you have these operations in pandas, and if you haven't, check out some other simpler projects we have in the platform that explain this in more detail. The idea is we are performing this operation

that returns a Boolean array, right? So again, refer to the project in the platform. We have a very, very thorough step-by-step process to explain these topics. But basically, this creating a Boolean volume as saying select this word, select this word, or select this word, and indicating if we wanna retrieve it or not with a false true value. So in this case, we don't want the AA word, we don't want the AAH word, et cetera. But the ones that fulfill or match this condition

will have a true value here. In this case, it's a long Boolean array, it's a 172,000 element Boolean array, and there's just one true volume, which is the one for this word. So once this Boolean array is resolved to put in a way in memory, we pass that Boolean array to.lock method, and that returns the value. So here is to kind of wrap it up with this idea of.lock, we have three ways of doing.lock. We can pass just one value as we did before for the index, right?

We can pass several values for that match the index, or we can pass a full Boolean array indicating which elements, which rows we want to select by just putting a false or a true volume. One tiny comment that's kind of a question we receive all the time is that is this efficient? We could have resolved this thing with just iterating and not using any memory to put in a way because at the end of the day, we have to create this Boolean array, which is gonna set in memory, and then we do the selection based on that Boolean array. So the question is, why are we gonna create this intermediate Boolean array if we could just, you know, each radius, right? I can do something like four row in df if row at value equals 319 print row,

just to put away in reality, have to also use the index. But anyway, in this case, I'm just doing a more imperative way of resolving this activity, and I'm not using any memory. I'm just, the data frame is data frame, there's existing memory. I create an auxiliary slot just for this particular row, and this seems to be more efficient. But the reality is that these Boolean arrays are actually very, very tiny. They might look like they are this huge, huge, you know,

series to all these string values, but in reality, these are just ones and zeros. It's a very small one bit element array, right? And the index is pretty much the same we have. So just a comment on the side, I don't wanna get too technical, but again, the beauty of panda selections, this dot lock works by the index, and again, we can pass just one value, multiple values, or a full Boolean array.

All right, moving forward, what is the most common value? To answer this question, we can refer back to, let me do value, scribe, there you go. And we can do different statistical methods, right? So in this case, we have the mean, standard deviation, minimum, maximum, et cetera. We also have the median, which is right here, which is a value 103. And there is one that is not showing up here, which is the mode of the given series or a given array.

So we could have an understanding of the statistical methods. So we're gonna do something like df at value mode. And we get that 93 is the most common value. Now, this doesn't tell you much, because it just tells you of all the different values we have in our data frame, let's do that. 93 is the value that shows up the most times. And we don't know in which situations, because the character count might be different, but there are different characters, so the value is different. But a very common method, something that I use all the time is the df at value, and we're gonna do value counts method.

This is a very, very helpful, I get just the first ones. A very helpful method is gonna give you this given value appears 1,965 times. So 100, as activity said, is the second most common value. And there are 1,921 words that match that value.

The value 93 is the most common one, and there are 1,965 words that match that given value right here. We could do something like df.lock df at the value equals 93, and we can get here, let's get the first 10. All these words have the same value, even though they have different chart counts, right? Because they have different characters. Let me do sample here, so we get a random sample of values, so right here we have, for example, in jests have seven characters and the value is 93. Again, the sum of the characters results in 93. But let's do it again, I'm gonna keep that one there.

The testable has 10 characters and has the same value, right? So again, to compute the mode of values, you can use the mode of statistical method, or for me, more common, very useful is the value counts method gives you not just the most common value, gives you kind of a ranking of the most common values in a column, and also gives you how many samples you can find of that value. All right, so, oh, we haven't done it. Let's see if it passes, 93 is our most common value. There you go, it worked. What is the shortest word with the value 274? Let's resolve that now.

df, we're gonna do a little bit of selection first, so let me align that so we can zoom in. What is the shortest word with the value 274? So first, we can start by selecting all the words with a value of 274, there you go. And then what we could do is either sort the values by char count, and this is gonna be the shortest one, this is gonna be the shortest one, or we could have done a little bit of more selection. I think that just by doing this method, it works.

We could have done something like we get the value, the char count, sorry, here. So we have that only the given column, and then we get the minimum value, right? So what's the minimum value, 20? With this thing, we could have also extended our query and do df.log, I'm gonna do that, and this, and the df.log, no, df at char count is equal to 20, that's the one we got before, and this is equal to that, and there we go,

we can do a very quick selection. We could have done it also in a way that it's dynamic, so we get plug this value here, and then we can do a very quick selection. So we get plug this value here, and this is all the same thing. At the end of the day, I think that just, if we don't have too many elements, sorting the values is an easier way to resolve it. So let's go ahead and do it, there we go. By the way, this is something that we do in production to put in a way like in our day-to-day jobs all the time. Sometimes you will need to build more dynamic solutions, but sometimes you're just doing some analysis, just exploring data, you can keep it easy, just with the different methods. You can hear the important pieces applying the right technique for the right problem. All right, moving forward.

Next activity, this is interesting. Create a column ratio, which represents the value ratio of a word, which is defined as the value of the word divided by the character count. So let's do that right now. Stop me if you wanna practice this by yourself, but the way to create this ratio is gonna be, let me show you the F first, though, head. It's gonna be df at ratio, that's the name that it's asking us, the column that it's asking us, is gonna be equals to df at value divided by df by chart count.

And now we're gonna do this thing again, and we have a ratio computed right here, the value divided by the chart count. Let's check to see if it works. It did work, we are good to keep moving forward. So a few more questions, what is the maximum value of ratio, df at ratio dot max, 22.5.

What word is the one with the highest ratio? And here, multiple ways to solve it, again, sort values, I think it's a good alternative. We're gonna do by ratio, but here, let's do it right here, in this way. By default, our values is sorting in ascending mode. We actually want it flipped, we wanna start with the highest one at the top. So we're gonna pass ascending equals false, we're gonna pass this order. And now we have that the highest ratio is 22.5.

What word is the one with the highest ratio? 22.5, the word is x, u. As we know, the highest ratio, we could also have written a query, df dot lock, where df at ratio is equals to the maximum ratio, and that is also x, u is the same result. So again, two different ways of resolving the same activity. It's important for you to have all the techniques in your tool belt, then which ones you use, it depends on what problem you're solving. How many words have a ratio, how many words have a ratio of 10? So what we can do here is we can do df,

we could have done, this is not gonna work so well, value counts, to get an idea of the values. And here we have just, this was, we were very lucky that 10 is a common enough value that there are 2,600 words with it, and we can see it. But if this was not such a common value, the ratio we're looking for, it's probably buried in this sea of different ratios because there are actually 1,333 different ratios. So as ratio is a continuum variable, there are multiple individual points, it's usually not convenient to use value counts. Instead, what we wanna do is just write a simple query, df.lock all the rows that have a ratio of 10, right?

That's the query we wanna build. Gives us a bunch of rows, what we can do is just do a shape, and that's gonna give us the same number that we have right here. Gonna plug it in here. Let's do a quick, quick stop here in asterisk to mention the.query method. So df.lock and all the selections, usually the prefer method because it's the most powerful one, but sometimes you just wanna use the query method. That is just kind of a shorthand. You can write it in plain English.

It's kind of Python-based, and the query method just works in this way. We can say ratio equals 10, and it's relatively simple. You can use it whenever you have simple conditions. There you go, here's for you to compare. I personally, myself, I usually prefer this method, but that's because I'm just very used to it, and I like thinking in terms of Boolean arrays and combining Boolean arrays and all that. Up to you if you wanna use query, the one that suits you better.

Moving forward, what is the maximum value of all the words with a ratio of 10? So we're gonna keep, let's keep query here. This is the results. And what we can do here is we can sort the values. Let me put in context again the activity. What is the maximum value? Sort of values by value. Value ascending false. And here we can find what is the maximum value of all the words with a ratio of 10, right? Head, there you go. We had, what is the maximum value? 240, it seems.

We could have also done here, actually for this particular solution, I think this method is gonna be better. We can do that, and we're gonna do value, just get the max. We should get the same result, 240. And that's the same answer here. The beauty of sort values is sometimes you see the data. In this case, you have to trust it, which is fine. We have an analytical resolution, we can trust it. But it is really like you can trust it. Like the ratio is 10, chart count is 24, the value is 240, it's usually a little bit, I don't know. It leaves you at peace to see that the data is okay.

Let's keep moving forward. Of those words with a value of 216, what is the lowest chart count found? In this case, we have pretty much the same thing. We're gonna do here. The F, let's do query now. And we're gonna do, of those words with the value is 260, so value is 260. What is the lowest chart count found? So first, this thing is gonna give you all the words with a value of 260. Now we can sort values by chart count.

And that's gonna give us, what is the lowest chart count found is 17. So here we're gonna find 17, there you go. And the last activity we're gonna be done based on the previous task, what word is it? This is the word and we are good to go. One important, one minimum comment about the query method and we're just gonna wrap it up with these particular projects is that when your columns have spaces,

you need to surround them in backticks. So let's get, let's invent a new activity just by ourselves here very quickly. I'm gonna use describe, there you go. Let's say I'm just writing this on the fly. On the fly, find all the words with chart count greater than the mean or the average chart counts. Chart counts, yeah, character counts. All right, so this activity we're like just making up on the fly.

How can we, first we can compute and let's say we have, we're gonna mean chart counts, it's gonna be count. It's gonna be.mean and we're gonna print it. I'm gonna show you how to write the query using this variable. We're gonna do df.query where chart count and this is not gonna work is greater than nine, right? So this is not gonna work because there are spaces you have to surround them with backticks. That's the first thing you wanna work. The second thing is if you wanna reference this variable in here, if I do this, that's not gonna work either, just gonna blow up because by default, anything that is a name within this query is gonna reference a column. So if you wanna reference an external variable,

what you have to do is pass here the ampersand symbol. In this case, gonna say, give me all the values in which this column surrounded by backticks, so respect the white space is greater than this thing and this thing is referencing an external variable. So go out to put it away to the global scope or local, whatever, and find this value, plug it in and run the query and here is what we can do right there. So again, the query method is convenient. I personally, myself, I prefer to use the.log method. I like to combine it with column selection.

I like to think in terms of Boolean arrays, but it's perfectly fine to use query. Just remember these tricks, right? That sometimes you're gonna need to use backticks for columns with white spaces and you can reference external variables with the add symbol. That is it with data frames. Let's keep moving forward and solving more projects applying those data science skills. In this project, we're gonna practice how to filter data, sort it, do some selections,

some querying, all very useful data analysis techniques. The data that we're gonna be working with is a dataset containing Pokemon information, including their type, their stats, some total attack, defense, and all that, their generation, and their legendary status. So let's go ahead and just read the data to get started and we're gonna start working with activities one by one. As usual, just pause if you wanna resolve it by yourself and then you're gonna hear me with the explanation.

What I'm gonna try doing in this project or for this project is giving you all the different possibilities we can think of. So solutions with.lock, solutions with ilock if that applies, and also solutions with the.query method. The first thing you have in this notebook is a little bit of an information in terms of distribution of the Pokemon we have. So for example, these are all the type one Pokemons and we also have some analysis of, for example, their stats. So for example, total, we see that there are a few very powerful Pokemons, both in terms of a histogram and a box spot. So all these visualizations are usually pretty useful, right? When you're starting your data analysis with a new dataset,

you just do a quick visualization of how your data is distributed in terms of, for example, categorical variables, how they are assigned, or also the ranges distribution of some numeric variables, like in this case, the total ones. So we can see the most powerful Pokemons. In this case, there are legendary status. We can also consider a categorical variable. And on that, let's get started with the activities one by one. So how many Pokemons exist with an attack value greater than 150, right?

So let's take a look at the data first. That's what we're dealing with. And we're going to focus how many Pokemons exist with an attack value greater than 150. So let's get started, pause if you want. And we're going to do df.lock df at attack greater than 150, right? That's going to give us the full list of Pokemons with that attack. And we can kind of see that there weren't like too many with that value already with the fox plot. With the query itself, we can get a real sense of how many values we have. In this case, we have just three. We could also do something like shape to get it. But again, we can clearly see that there are only three samples that match that given condition. We can also do the query method.

So I can do df.query where attack is greater than 150. And we get the same results. So let's try it out. See, there you go. It worked, correct. Moving forward, select all the Pokemons with a speed of 10 or less. So in this case, we need to create a sub data frame. So it says right here, story your results in slow Pokemons.df. So in this case is a speed of 10 or less. So that's gonna be pretty, pretty slow. It's right below here. We have probably 10. It's gonna be a pretty small amount of Pokemons. So let's take a look first, how many fulfill that condition. speeddf at, oh, mis-type. Speed is 10 or less. So this is less or equals and 10.

And we have only a handful, one, two, three, four, five. And we can of course assign that to this variable to have a final check with activity. Let's do it on the side in parallel. There you go, it worked. And of course using.query, its query speed is less or equals than 10. From now on, I will try to stick to the.lock method is the one that I prefer. Instead of using query, but you can use either. I prefer to work with Boolean arrays. Remember this expression,

it's gonna be just a Boolean expression, right? So how many of these values are, the values are true are basically that the values that match the given condition. This is pretty useful if we wanna count the values. And this is something that I could have done in the previous one, which I didn't realize showing before. Basically, if I did something like this.sum, I'm gonna get the same result, right? How many values in this full array, so array is not, attack is an array,

let's say a series, a collection that has integers. And then we ask, give me an array, a Boolean array of all the ones that match this condition, this case greater than 150. So it's gonna be false, false, false, false, false. And of course, we're gonna have three in this huge array that are true. So in this case, as I told you in the previous project that we did with English words, if the array, in this case, it seems that there are just, you know, strings saying false and true,

in reality, this is a Boolean array. So in reality, these are all ones and zeros, very efficient, very compact array, ones and zeros. We can sum that and we have the final result. In this case, we can count, we can sum, write all the ones, we have just three ones and that gives us the answer to the previous question as well. All right, moving forward, how many, let's go here, how many Pokemons have a SP depth, value, special defense, value of 25 or less, special ability defense.

So we're gonna do df.log, how many Pokemons have a special defense value of 25 or less? We're gonna do df at sp.def less or equals than 25, because again, it's a value of 25 or less. And now we have a bunch. So now we can use the technique that we did before, that is just this.sum, gonna crease the sides here a bit. And we have 17 as a result. I could have done the same thing here, shape, and I get pretty much this table has 17 rows. Of course, this is a little bit more compact, let's say easier. So let's try it out, 17 and see if it works. There you go, they work, we're good to go and keep moving progress, making progress.

So select all the legendary Pokemons. This is pretty interesting one. We have to sort the result, select only Pokemons that are legendary and sort the result in the variable legendary.lgdf. So that's gonna be df.log, df at legendary, but let's take a look first using the info method. Let's take a look at the legendary column. The legendary column itself is a Boolean column, a Boolean series given by its type. So that means that it is itself a Boolean array, with true and false values.

We could ask df at legendary, how many legendary Pokemons we have in this dataset, and that's just the sum of the legendary status. And that's it, because this is a Boolean array. So remember, if we did df at attack, for example, greater than 150, and we had this huge Boolean array, false true, false true, and then we selected the ones that had a true value, we can pretty much do the same thing here. And what I'm gonna do is, we could say something like, this thing is true, right?

So give me all the legendary Pokemons. What did I do wrong? False kind of, oh, Boolean index, df is equals, there you go, my bad, is equals to true. But the reality is that this column itself is a Boolean array. So let's just get rid of the true, and we say, select all the Pokemons that are legendary, just that, just pass the mask, the Boolean mask, and perform the selection. If we wanted, let's say we wanted to get all the Pokemons that are not legendary, the non-legendary Pokemons. We can, of course, do something like false, we're gonna get how many we have.

But the reality is we can just invert the array. We had false, false, true, false, right? That was our original array. We can do inversion of this thing, negate it with this operator. That's gonna give us true, true, false, true. This is false. So what I can do is I can just do the negation here, and I get the same result. So sorry, I got a little bit sidetracked, but what we wanna get is all the Pokemons are of type legendary. And to do that, we're gonna assign that to this variable.

And we're gonna get the head, first five rows. There you go, all legendary. And let's check if the activity passes. This is why it's so important to understand the column types of your data. Because sometimes the selections or the Boolean arrays you're gonna be working with are not the same depending on the type of the data you have. Okay, moving forward, find the outlier. So we have this distribution of Pokemons, and we have to find this particular one, the one that has a difference value that is really big and also an attack value that it's pretty low, right? So this Pokemon has a ton of defense, but a very low attack. Of course, we could do something like df.defense.sort.

Let's do df.hand, show the whole thing. Then we're gonna do df.sort values by defense, and show the first view. By default, the sorting method is in ascending mode. So we are starting in this way, we're going in this direction. We wanna invert that, we're gonna do ascending equals false. So now we have the strongest one here. And here we have the one that has a defense of 230 and an attack, right? Of, in this case, attack here of 10. This pretty much gives us the answer right away. Let's actually try it out. Let's see if this is the one, this is the Pokemon that activity requests,

at least for this particular value, it is that passed. But I wanna make a comment here is that you can actually sort by multiple criteria. We could also say something that sort by defense first, and then as a second criteria, sort by attack. There you go. And then for defense, we want it to be in descending mode. So ascending is false, but for attack, we want it to be in ascending mode. Again, the result is the same because the data, it's pretty clear who is this outlier right here.

But again, the understanding is we can sort by multiple criteria. What is gonna happen is that the sorting starts here, and then we're gonna sort by attack in the other direction. We can combine as many as we want of this criteria right here. All right, moving forward, more activities. Now we're gonna get into advanced selection with some Boolean conditions. How many far fly Pokemons are there? So we want type one equals equals fire and type two equals flying. So what we're gonna do is we're gonna do df.lock. And here, again, pause if you want, we're gonna do a little bit of Boolean operators. The first condition is gonna be df at type one is equals to fire, right? And the second condition, and how many do we have?

The second condition is that type two has to be flying. Flying, there you go. But what we want is the combination of both, type one fire and type two flying. The way to do that is to combine them with an operator, an ampersand operator. So I'm gonna break it into different lines here. And I'm gonna put the operator right here so you see the conditions. How many far flying Pokemons are there? Seems like we only have five. Let's try the result first and then we're gonna explore a little bit more

the data frame resulting. And as you can see, I haven't even looked at a single Pokemon. What I'm doing is just combining conditions and summing Boolean arrays. So this is a pretty interesting thing because what is happening here is like, let's say we had only five Pokemons in total, right? We have the first array here is gonna say type is fire and the second array is gonna be is flying, right? And we had something like true, true, false, true, false.

So this is of type fire, type fire, not type fire. Yes, type fire, not type fire. And then for flying, we had false, true, false, false, false. There we go. So in this case, this is the only flying Pokemon. What happens and that's why we have like different results. Of course, we had here a bunch of fire Pokemons and here we have a cup of a bunch of flying Pokemons. We have true values for the condition. But what happens when we use this ampersand is that we do one by one, bit by bit, Boolean value by Boolean value, we do the operation, the ampersand operation, which is like the and operation in Python. So we do true and false for the first one. So the first one can be true and false.

What is the result of true and false? False. And what then we do the result of true and true. What is the result of true and let me show you true and false is of course false. True and true is true. So we have that false and false is false. True and false again is false. And we could say this one was true. False and true is false. There we go. So in an and operation and we have operator one and operator two or operand, operand one and operand two, for this expression, for this whole thing, for this whole expression to be true, both OP one and OP two must,

well put it this way, must be true for this operation to be true. Anything else either OP one is false or OP two is false or both are false. Anything else, anything else is false. This is just some Boolean arithmetic, Boolean operators just, you know, a little bit that applies to Python as well. But the interesting thing is that that same operation we're seeing here, comparison bit by bit, value by value, a line is what we're doing right here. So we had 47 Pokemons were of type one fire and we had 89 Pokemons were of type two flying. And then we combined those things using an ampersand, an and operation and the result was only five Pokemons. So we can actually visualize those sample points.

We're gonna do dot lock and we're gonna pass that condition. And these are the only five Pokemons are of type one fire and type two flying, okay? Because we are using these Boolean operators. Okay, moving forward. How many Pokemons, oh no, one thing, one thing. If we wanted to use a query method, we could have rewritten this thing with query and we're gonna do, remember that if you have white spaces in your column names, you have to surround that with backticks.

In this case is gonna be fire and type two, type two equals flying. And I made a mistake here. Flying should be a string, there we go. We have the final result. So again, this is easier to type probably. We need to put the backticks in the column that has a white space. And here we can just use Python and we can use a string. You have to make sure you're matching or on the contrary, don't matching the string quotes you're using to surround the whole expression. And then within the Boolean operators, we use the regular Python ones. We don't use the and percent, we use just the and. When we are using dot lock form, we must use the Boolean operators are and or a not.

And if we're doing something here, let me see if I can do a not. Yes, we can do a not right here as well. Okay, now moving forward, activity number seven. How many Poisson Pokemons are across both types? So as you can see, right here, the type of a Pokemon is a Pokemon has two types. Let's say it has a strong type and a secondary type. The type poison can be applied to either the primary type or the secondary one, because I can actually show you that to you type one counts. We have that there are poison 28 Pokemons that have poison in type one and type two.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent