turicreate.SFrame.filter_by¶
-
SFrame.
filter_by
(values, column_name, exclude=False)¶ Filter an SFrame by values inside an iterable object. Result is an SFrame that only includes (or excludes) the rows that have a column with the given
column_name
which holds one of the values in the givenvalues
SArray
. Ifvalues
is not an SArray, we attempt to convert it to one before filtering.Parameters: - values : SArray | list | numpy.ndarray | pandas.Series | str | map
- | generator | filter | None | range
The values to use to filter the SFrame. The resulting SFrame will only include rows that have one of these values in the given column.
- column_name : str
The column of the SFrame to match with the given values.
- exclude : bool
If True, the result SFrame will contain all rows EXCEPT those that have one of
values
incolumn_name
.
Returns: - out : SFrame
The filtered SFrame.
Examples
>>> sf = turicreate.SFrame({'id': [1, 2, 3, 4], ... 'animal_type': ['dog', 'cat', 'cow', 'horse'], ... 'name': ['bob', 'jim', 'jimbob', 'bobjim']}) >>> household_pets = ['cat', 'hamster', 'dog', 'fish', 'bird', 'snake'] >>> sf.filter_by(household_pets, 'animal_type') +-------------+----+------+ | animal_type | id | name | +-------------+----+------+ | dog | 1 | bob | | cat | 2 | jim | +-------------+----+------+ [2 rows x 3 columns] >>> sf.filter_by(household_pets, 'animal_type', exclude=True) +-------------+----+--------+ | animal_type | id | name | +-------------+----+--------+ | horse | 4 | bobjim | | cow | 3 | jimbob | +-------------+----+--------+ [2 rows x 3 columns]
>>> sf.filter_by(None, 'name', exclude=True) +-------------+----+--------+ | animal_type | id | name | +-------------+----+--------+ | dog | 1 | bob | | cat | 2 | jim | | cow | 3 | jimbob | | horse | 4 | bobjim | +-------------+----+--------+ [4 rows x 3 columns]
>>> sf.filter_by(filter(lambda x : len(x) > 3, sf['name']), 'name', exclude=True) +-------------+----+--------+ | animal_type | id | name | +-------------+----+--------+ | dog | 1 | bob | | cat | 2 | jim | +-------------+----+--------+ [2 rows x 3 columns]
>>> sf.filter_by(range(3), 'id', exclude=True) +-------------+----+--------+ | animal_type | id | name | +-------------+----+--------+ | cow | 3 | jimbob | | horse | 4 | bobjim | +-------------+----+--------+ [2 rows x 3 columns]