Loading...

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

123
id
Integer
100%
...
123
score
Numeric(21)
100%
Abc
extra_info
Varchar(5)
100%
112...9.0Grey
211...9.5Black
313...4.0White

To focus only on the quantities of interest, we can utilize the narrow function:

vdf.narrow("id", col_name = "state", val_name = "score")
123
id
Integer
100%
...
Abc
state
Varchar(5)
100%
123
score
Numeric(21)
100%
112...state12.0
211...state11.0
313...state13.0
412...size100.0
511...size120.0
613...size140.0
712...score9.0
811...score9.5
913...score4.0

Note

The inverse function of pivot is narrow. 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.