pivot

dataframe

Creates a pivot table from a DataFrame

Syntax

pivot(dataframe, index, columns, values)

Parameters

dataframe (dataframe)

The DataFrame to pivot

index (string)

Column to use as row index

columns (string)

Column to use as column headers

values (string)

Column containing values to aggregate

Returns

dataframe

Pivoted DataFrame

Examples

Create pivot table of sales by date and category
Input:
pivot("date", "category", "amount")
Output:
Pivot table with dates as rows and categories as columns
Pivot monthly sales data by product
Input:
pivot("month", "product", "revenue")
Output:
Pivot table with months as rows and products as columns

The pivot() function creates a pivot table by reshaping data from long to wide format. It uses one column for row indices, another for column headers, and a third for values.

Usage

Use pivot() to create cross-tabulations, transform time-series data for analysis, or create summary tables. This is the inverse operation of melt().

Related Functions