Filtering using the isin method

Another way to filter data is by using the isin method. We can use the isin method to filter our dataset by a list of values for a particular column or columns. Here, we select only those records from the Metro column that have values of either New York or San Francisco.

We call the isin method on the Metro column, and pass it a list containing the cities we want to select. This will create a Boolean series. We then pass the Boolean series to our dataset DataFrame to make the necessary filtering and selection, as follows:

filter = data['Metro'].isin(['New York', 'San Francisco']) 

The following screenshot shows the filtered data with records from the two cities of New York and San Francisco ...

Get Mastering Exploratory Analysis with pandas now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.