When you’re working on a task, you often just need part of the data, or a data slice.
One way to get that slice is to apply a filter. One option for that filter is a Boolean array with True and False values. True value will mark the lines we want to include in our data slice, while False will indicate values that we don’t want to include. Setting that filter up by hand would be exhausting if not practically impossible for large data sets, so pandas uses automatic filters. Below is an example of an automatic filter.
data['column'] == 'value'
That filter will serve as a DataFrame index:
data[data['column'] == 'value']
It's not just the equals sign that can serve as the condition for creating a filter; we can also use comparisons like !=, >, >=, <, and <=. We can compare the values in the columns with any assigned values and each other:
data['column1'] > data['column2']
We can use arithmetic operations in the conditions:
data['column1'] / 2 > data['column2'] + 0.5
To see whether there are any specific values in the column, we call the isin(): ** method
data['column'].isin(('value1', 'value2', 'value3'))
Sometimes you need to get a slice that meets multiple conditions at the same time, which is what we have logical operations for. Separate conditions are listed in parentheses:
Another, simpler way to get slices is the query() method. We put the condition for the slice in the string given as an argument for the query() method, then applying it to the DataFrame. That gets us the slice we’re looking for.
data.query('column == value')
The conditions set in the query() parameter*:*
—Support different comparative operations like !=, >, >=, <, and <= as well as various mathematical operations