Boolean Mask Workout
- 01:45
Practice using Boolean Masks.
Downloads
No associated resources to download.
Transcript
Let's get some practice with boolean masks. First, make sure that you've imported stock data csv into the data frame stock data. Then let's complete the following steps. First, display the first three rows of stock data so that we can see what we're working with. Then using a bullion mask, create a new series in stock data that's an indicator variable for information technology companies. Finally display the first three rows of stock data again, so that we can see the changes.
As always, I'm starting by importing Panda and creating the stock data data frame, which if you've done this in your Jupyter Notebook already, is an unnecessary step. First, I'm going to display the first three rows of stock data using the head function, and then I'm going to create a new series in that data frame using a Boolean mask. So I'm calling this new series tech industry and I'm defining it as this boolean mask. So I'm using stock data sector, and remember, this is the second method of pointing to a series within a data frame, and it works because sector is only one word. We don't have spaces like we would in tech industry, so we can use this dot method, which is a little bit more concise. So stock data, the sector series, we're checking to see if that's equal to information technology. And then the values in this boolean mask are gonna be created using the integer object type. So it's gonna come out as zeros and ones.
When we display the first three rows, we can see that the tech industry series has been created and it appears to be working correctly.