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) –
VastColumnused to group the elements.columns (str) – The
VastColumnused 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), writeMAX({}) - MIN({}).prefix (str, optional) – The prefix for the pivot table’s column names.
- Returns:
the pivot table object.
- Return type:
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 fromvastorbitare 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], } )
AbcdateVarchar(10)AbccatVarchar(1)123saleInteger1 2014-01-01 A 100 2 2014-01-02 A 120 3 2014-01-02 B 110 4 2014-01-01 B 120 Rows: 1-4 | Columns: 3To better view the data, we can create a pivot table:
vdf.pivot( index = "date", columns = "cat", values = "sale", aggr = "avg", )
AbcdateVarchar(10)123ADouble123BDouble1 2014-01-02 120.0 110.0 2 2014-01-01 100.0 120.0 Rows: 1-2 | Columns: 3Note
The inverse function of
pivotisnarrow. With both, you can preprocess the table either vertically or horizontally. These functions utilize pure SQL statements to perform the job.