datascience.tables.Table.where

Table.where(column_or_label, value_or_predicate=None, other=None)[source]

Return a Table of rows for which the column is value or a non-zero value.

If column_or_label contains Boolean values, returns rows corresponding to True.

Args:

column_or_label: The header name of a column in the table or an array.

value_or_predicate: Value to compare to items in column or function to apply to items in column.

Returns:
An instance of Table containing rows for which the column_or_label column or column_or_label itself is non-zero or True, or is equal to value, if provided.
>>> marbles = Table().with_columns([
...    "Color", ["Red", "Green", "Blue", "Red", "Green", "Green"],
...    "Shape", ["Round", "Rectangular", "Rectangular", "Round", "Rectangular", "Round"],
...    "Amount", [4, 6, 12, 7, 9, 2],
...    "Price", [1.30, 1.20, 2.00, 1.75, 1.40, 3.00]])
>>> marbles
Color | Shape       | Amount | Price
Red   | Round       | 4      | 1.3
Green | Rectangular | 6      | 1.2
Blue  | Rectangular | 12     | 2
Red   | Round       | 7      | 1.75
Green | Rectangular | 9      | 1.4
Green | Round       | 2      | 3
>>> marbles.where("Shape", "Round")
Color | Shape | Amount | Price
Red   | Round | 4      | 1.3
Red   | Round | 7      | 1.75
Green | Round | 2      | 3
>>> marbles.where(marbles.column("Shape") == "Round") # equivalent to previous example
Color | Shape | Amount | Price
Red   | Round | 4      | 1.3
Red   | Round | 7      | 1.75
Green | Round | 2      | 3
>>> marbles.where(marbles.column("Price") > 1.5)
Color | Shape       | Amount | Price
Blue  | Rectangular | 12     | 2
Red   | Round       | 7      | 1.75
Green | Round       | 2      | 3

You can also use predicates to simplify single-column comparisons.

>>> from datascience.predicates import are
>>> marbles.where("Price", are.above(1.5)) # equivalent to previous example
Color | Shape       | Amount | Price
Blue  | Rectangular | 12     | 2
Red   | Round       | 7      | 1.75
Green | Round       | 2      | 3

And apply some predicates to compare columns.

>>> marbles.where("Price", are.above, "Amount")
Color | Shape | Amount | Price
Green | Round | 2      | 3