verticapy.vDataFrame.search¶
- vDataFrame.search(conditions: Annotated[str | list[str] | StringSQL | list[StringSQL], ''] = '', usecols: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None, expr: Annotated[str | list[str] | StringSQL | list[StringSQL], ''] | None = None, order_by: None | str | dict | list = None) vDataFrame¶
Searches for elements that match the input conditions. This method will return a new vDataFrame.
Parameters¶
- conditions: SQLExpression, optional
Filters of the search. It can be a list of conditions or an expression.
- usecols: SQLColumns, optional
vDataColumns to select from the final vDataFrame relation. If empty, all vDataColumns are selected.
- expr: SQLExpression, optional
List of customized expressions in pure SQL. For example: ‘column1 * column2 AS my_name’.
- order_by: str / dict / list, optional
List of the vDataColumns used to sort the data, using asc order or a dictionary of all sorting methods. For example, to sort by “column1” ASC and “column2” DESC, write:
{"column1": "asc", "column2": "desc"}
Returns¶
- vDataFrame
vDataFrame of the search
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 fromverticapyare 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.
We can create a custom search that is looking for the family size and survival of the passengers having adults of more than 50 year old. We can arrange the data in descending order to see who paid the most:
vdf.search( conditions = ["age > 50"], usecols = ["fare", "survived"], expr = ["parch + sibsp + 1 AS family_size"], order_by = {"fare": "desc"}, ) Out[4]: None fare survived 1 512.3292 1 2 263.0 0 3 263.0 1 4 262.375 0 5 221.7792 0 6 221.7792 0 7 164.8667 0 8 153.4625 1 9 146.5208 0 10 146.5208 1 11 135.6333 1 12 113.275 0 13 93.5 0 14 93.5 1 15 83.1583 1 16 83.1583 1 17 81.8583 1 18 81.8583 1 19 80.0 1 20 79.65 0 ... ... ... Rows: 1-20 of 95 | Columns: 2
123fare98%123survived100%1 512.3292 1 2 263.0 0 3 263.0 1 4 262.375 0 5 221.7792 0 6 221.7792 0 7 164.8667 0 8 153.4625 1 9 146.5208 0 10 146.5208 1 11 135.6333 1 12 113.275 0 13 93.5 0 14 93.5 1 15 83.1583 1 16 83.1583 1 17 81.8583 1 18 81.8583 1 19 80.0 1 20 79.65 0 Note
Similarly, the same can be done in a Pandas-like way:
vdf.search( conditions = vdf["age"] > 50, usecols = ["fare", "survived"], expr = ["parch + sibsp + 1 AS family_size"], order_by = {"fare": "desc"}, )