Loading...

vastorbit.VastFrame.narrow

VastFrame.narrow(index: Annotated[str | list[str], 'STRING representing one column or a list of columns'], columns: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None, col_name: str = 'column', val_name: str = 'value') VastFrame

Returns the Narrow Table of the VastFrame using the input VastColumns.

Parameters:
  • index (SQLColumns) – Index(es) used to identify the Row.

  • columns (SQLColumns, optional) – List of the VastColumns names. If empty, all VastColumns except the index(es) are used.

  • col_name (str, optional) – Alias of the VastColumn representing the different input VastColumns names as categories.

  • val_name (str, optional) – Alias of the VastColumn representing the different input VastColumns values.

Returns:

the narrow table object.

Return type:

VastFrame

Examples

Let’s begin by importing vastorbit.

import vastorbit as vo

Hint

By assigning an alias to vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit 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 vastorbit are used as intended without interfering with functions from other libraries.

For this example, let’s generate a dataset which has multiple columns:

vdf = vo.VastFrame(
    {
        "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
123
state
Integer
123
size
Integer
123
score
Decimal(11, 1)
Abc
extra_info
Varchar(5)
111111209.5Black
213131404.0White
312121009.0Grey
Rows: 1-3 | Columns: 5

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
Abc
state
Varchar(5)
123
score
Decimal(11, 1)
111score9.5
212state12.0
313state13.0
412score9.0
513size140.0
612size100.0
711size120.0
811state11.0
913score4.0
Rows: 1-9 | Columns: 3

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

VastFrame.pivot() : Pivots the VastFrame.