pandas Applying a boolean mask to a dataframe


Example

This will be our example data frame:

  color      name   size
0   red      rose    big
1  blue    violet    big
2   red     tulip  small
3  blue  harebell  small

Using the magic __getitem__ or [] accessor. Giving it a list of True and False of the same length as the dataframe will give you:

df[[True, False, True, False]]
  color   name   size
0   red   rose    big
2   red  tulip  small