August 2019
Beginner
482 pages
12h 56m
English
Now, both loc and simple square brackets accept masks. Mask can be represented by a Series, a NumPy array, or a simple list of Boolean values of the same length as the number of rows in the dataframe. If given, this collection will be interpreted as a mask—essentially, an explanation of which rows to return. For example, we can use our third column, z, as a mask to filter on. Because we only have a True value in the first row, a dataframe of one row will be returned:
>>> df[df['z']] x y z new_column1 2 b True -1
This is a very important technique, which we'll be using all the time! Such a mask can be generated using any logic operations, for example, an equality operator. Take a look: here, we are creating a mask by checking whether ...