Loading...

vastorbit.VastFrame.pivot

VastFrame.pivot(index: str, columns: str, values: str, aggr: str = 'sum', prefix: str | None = None) VastFrame

Returns the Pivot of the VastFrame using the input aggregation.

Parameters:
  • index (str) – VastColumn used to group the elements.

  • columns (str) – The VastColumn used to compute the different categories, which then act as the columns in the pivot table.

  • values (str) – The VastColumn whose values populate the new VastFrame.

  • aggr (str, optional) – Aggregation to use on ‘values’. To use complex aggregations, you must use braces: {}. For example, to aggregate using the aggregation: x -> MAX(x) - MIN(x), write MAX({}) - MIN({}).

  • prefix (str, optional) – The prefix for the pivot table’s column names.

Returns:

the pivot 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 dummy dataset representing sales of two items for different dates:

vdf = vo.VastFrame(
    {
        "date": [
            "2014-01-01",
            "2014-01-02",
            "2014-01-01",
            "2014-01-02",
        ],
        "cat": ["A", "A", "B", "B"],
        "sale": [100, 120, 120, 110],
    }
)
Abc
date
Varchar(10)
Abc
cat
Varchar(1)
123
sale
Integer
12014-01-01A100
22014-01-02A120
32014-01-02B110
42014-01-01B120
Rows: 1-4 | Columns: 3

To better view the data, we can create a pivot table:

vdf.pivot(
    index = "date",
    columns = "cat",
    values = "sale",
    aggr = "avg",
)
Abc
date
Varchar(10)
123
A
Double
123
B
Double
12014-01-02120.0110.0
22014-01-01100.0120.0
Rows: 1-2 | 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.narrow() : Narrow Table for a VastFrame.