Sets Workout
- 03:01
How to work with sets in Python, specifically focusing on performing union, intersection, and difference operations to analyze data sets.
Downloads
No associated resources to download.
Transcript
Let's get some practice working with sets. In your Jupyter Notebook file, you'll find the following two groups of companies, healthcare companies, and companies with a beta of greater than one. First create a set for each group of companies, and then using the union intersection and difference functions answer the following three questions. First, what are all of the companies included in both sets? Second, which healthcare companies have a beta of greater than one. And third, which healthcare companies have a beta of less than one.
In your Jupyter Notebook file, you have these healthcare companies and companies with the beta of greater than one, and we're gonna put these into sets and perform the union intersection and difference functions to answer the questions and exercise one. So first of all, let's create those sets. So I'm gonna call this one healthcare and it's gonna be contained in curly braces to tell Python that it's a set. I'm just gonna go up here and copy these since they're already formatted like a string and paste that in my curly braces. And then my next line, I'm gonna do the exact same thing.
The first thing we're asked to do is print out all of the companies in both of the sets, and we do that using the intersection or rather the union function. It doesn't matter which order that we put the sets in. So I'm gonna start with healthcare, and then I'm gonna write union, and in parentheses, beta my next set. And when I print that out, you see all of the companies that we have in both sets.
Now we want to know how many healthcare companies have a beta of greater than one. And we can find that answer by finding the intersection of both of these sets. What do these sets have in common? Which companies are both healthcare companies and also have a beta of greater than one? Similarly, we don't need to worry about the order of the sets. So I'm gonna type healthcare intersection and in beta, and that tells me which companies are in both lists. So these three have or are both healthcare companies and have a beta of greater than one. Finally, we want to know which healthcare companies have a beta of less than one. And we do that using the difference function. And in this case, order does matter. I'm gonna write healthcare difference and in parentheses, beta, and that is gonna give me my healthcare companies, which are not included in the set beta. If I flip that around, it's gonna gimme a different answer. So let's say beta difference healthcare, and that's gonna give me the companies in beta that are not included in the set healthcare. So order when you're using the difference function does matter.