Loading...

verticapy.vDataFrame.sort#

vDataFrame.sort(columns: str | list[str] | dict) vDataFrame#

Sorts the vDataFrame using the input vDataColumn.

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 from verticapy 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'],
    },
)

123
sales
Integer
100%
Abc
cat
Varchar(1)
100%
110C
211B
39A
420A
56B

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
123
sales
Integer
100%
Abc
cat
Varchar(1)
100%
16B
29A
310C
411B
520A

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
123
sales
Integer
100%
Abc
cat
Varchar(1)
100%
120A
211B
310C
49A
56B

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

vDataFrame.append() : Append a vDataFrame with another one or an input_relation.