From the course: Data Analysis with Python and Pandas

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Pro tip: The query() method

Pro tip: The query() method

- [Instructor] All right, so let's go ahead and take a look at one of my favorite data frame methods in Pandas. The query method lets us use SQL like syntax to filter data frames. We can specify any number of filtering conditions in a single line by using the 'and' and 'or' keywords. Here I'm calling the query method on our retail data frame, and I'm passing through the query, family in cleaning or dairy. And sales is greater than zero. So I probably don't need to explain the syntax to you given how intuitive it is. But what we're doing is filtering down to rows where our family is either cleaning or dairy, and our sales are greater than zero. And if we take a look at our results, we can see that our family column only has values cleaning and dairy, and we don't see any zero values for sales. Particularly if you're coming from an SQL background. This type of syntax is going to feel very comfortable and probably feel a little bit more intuitive than the Boolean mask building that we've…

Contents