verticapy.vDataFrame.sort#
- vDataFrame.sort(columns: str | list[str] | dict) vDataFrame #
Sorts the
vDataFrame
using the inputvDataColumn
.Parameters#
- columns: SQLColumns | dict
List of the
vDataColumn
used to sort the data, using asc order or dictionary of all sorting methods. For example, to sort by “column1” ASC and “column2” DESC, write:{"column1": "asc", "column2": "desc"}
Returns#
- vDataFrame
self
Examples#
Let’s begin by importing 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 fromverticapy
are used as intended without interfering with functions from other libraries.Let us create a
vDataFrame
which we can sort:vdf = vp.vDataFrame( { "sales": [10, 11, 9, 20, 6], "cat": ['C', 'B', 'A', 'A', 'B'], }, )
123salesInteger100%AbccatVarchar(1)100%1 10 C 2 11 B 3 9 A 4 20 A 5 6 B We can conveniently sort the
vDataFrame
using a particular column:vdf.sort({"sales": "asc"}) Out[3]: None sales cat 1 6 B 2 9 A 3 10 C 4 11 B 5 20 A Rows: 5 | Columns: 2
123salesInteger100%AbccatVarchar(1)100%1 6 B 2 9 A 3 10 C 4 11 B 5 20 A The same operation can also be performed in descending order.
vdf.sort({"sales": "desc"}) Out[4]: None sales cat 1 20 A 2 11 B 3 10 C 4 9 A 5 6 B Rows: 5 | Columns: 2
123salesInteger100%AbccatVarchar(1)100%1 20 A 2 11 B 3 10 C 4 9 A 5 6 B Note
Sorting the data is crucial to ensure consistent output. While Vertica forgoes the use of indexes for enhanced performance, it does not guarantee a specific order of data retrieval.
See also