verticapy.vDataFrame.narrow#
- vDataFrame.narrow(index: str | list[str], columns: str | list[str] | None = None, col_name: str = 'column', val_name: str = 'value') vDataFrame #
Returns the Narrow Table of the vDataFrame using the input vDataColumns.
Parameters#
- index: SQLColumns
Index(es) used to identify the Row.
- columns: SQLColumns, optional
List of the vDataColumns names. If empty, all vDataColumns except the index(es) are used.
- col_name: str, optional
Alias of the vDataColumn representing the different input vDataColumns names as categories.
- val_name: str, optional
Alias of the vDataColumn representing the different input vDataColumns values.
Returns#
- vDataFrame
the narrow table object.
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.For this example, let’s generate a dataset which has multiple columns:
vdf = vp.vDataFrame( { "id": [12, 11, 13], "state": [12, 11, 13], "size":[100, 120, 140], "score": [9, 9.5, 4], "extra_info": ['Grey', 'Black', 'White'], } )
123idInteger100%... 123scoreNumeric(21)100%Abcextra_infoVarchar(5)100%1 12 ... 9.0 Grey 2 11 ... 9.5 Black 3 13 ... 4.0 White To focus only on the quantities of interest, we can utilize the
narrow
function:vdf.narrow("id", col_name = "state", val_name = "score")
123idInteger100%... AbcstateVarchar(5)100%123scoreNumeric(21)100%1 12 ... state 12.0 2 11 ... state 11.0 3 13 ... state 13.0 4 12 ... size 100.0 5 11 ... size 120.0 6 13 ... size 140.0 7 12 ... score 9.0 8 11 ... score 9.5 9 13 ... score 4.0 Note
The inverse function of
pivot
isnarrow
. With both, you can preprocess the table either vertically or horizontally. These functions utilize pure SQL statements to perform the job.See also
vDataFrame.
pivot()
: Pivots the vDataFrame.