Loading...

verticapy.vDataFrame.filter#

vDataFrame.filter(conditions: None | list | str = None, *args, **kwargs) vDataFrame#

Filters the vDataFrame using the input expressions.

Parameters#

conditions: SQLExpression, optional

List of expressions. For example, to keep only the records where the vDataColumn ‘column’ is greater than 5 and less than 10, you can write: ['"column" > 5', '"column" < 10'].

force_filter: bool, optional

Default Value: True When set to True, the vDataFrame will be modified even if no filtering occurred. This parameter can be used to enforce filtering and ensure pipeline consistency.

raise_error: bool, optional

Default Value: False If set to True and the input filtering is incorrect, an error is raised.

Returns#

vDataFrame

self

Examples#

We import verticapy:

import verticapy as vp

Hint

By assigning an alias to verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions from verticapy are used as intended without interfering with functions from other libraries.

For this example, we will use the Titanic dataset:

from verticapy.datasets import load_titanic

vdf = load_titanic()

Note

VerticaPy offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the VerticaPy environment.

Using filter, we can create custom filters:

vdf.filter("sex = 'female' AND pclass = 1")
123
pclass
Int
100%
...
123
survived
Int
100%
Abc
home.dest
Varchar(100)
57%
11...0Montreal, PQ / Chesterville, ON
21...0Montreal, PQ / Chesterville, ON
31...0Montreal, PQ / Chesterville, ON
41...0Belfast, NI
51...0Montevideo, Uruguay
61...0New York, NY
71...0New York, NY
81...0Montreal, PQ
91...0Winnipeg, MN
101...0San Francisco, CA
111...0Trenton, NJ
121...0London / Winnipeg, MB
131...0Pomeroy, WA
141...0Omaha, NE
151...0Philadelphia, PA
161...0Washington, DC
171...0[null]
181...0New York, NY
191...0Montevideo, Uruguay
201...0Montevideo, Uruguay

Note

Similarly, the same can be done in a Pandas-like way:

vdf.filter((vdf["sex"] == "female") && (vdf["pclass"] == 1))

Or:

vdf = vdf[(vdf["sex"] == "female") && (vdf["pclass"] == 1)]

Warning

Ensure to use the && operator and correctly place parentheses. The and operator is specific to Python, and its behavior cannot be changed.

See also

vDataFrame.balance() : Balances the vDataFrame.
vDataFrame.drop() : Drops the vDataFrame input columns.