Skip to content

Pivot a Table

Your sales file has one row per sale, and you want a summary with one row per region and one column per month. Pivot Table turns the values of one column into new columns and fills each cell by aggregating a value column.

You'll use: File Input → Pivot Table → File Output.

  1. 1Input
    File Input
    sales_long.csv
  2. 2Transform
    Pivot Table
    region × month · sum of amount
  3. 3Output
    File Output
    sales_wide

Sample data

Save this as sales_long.csv. North has two January sales, and South has no February sale.

csv
region,month,amount
North,Jan,100
North,Feb,120
South,Jan,80
North,Jan,30

Steps

  1. On Datasets, upload sales_long.csv.
  2. In a new flow, add File Input. Set File type to CSV and Dataset to sales_long.csv.
  3. Add Pivot Table and set:
    • Index columns: region. Each region becomes one output row.
    • Columns from: month. Each distinct month becomes a column.
    • Values from: amount. These values fill the cells.
    • Aggregation: sum. Rows that land in the same cell are added up.
  4. Add File Output. Set File type to CSV (.csv) and Dataset name to sales_wide. Connect the nodes in order and click Run.

Pivot Table turns month values into columns: the two North January rows, 100 and 30, sum into one cell of 130.0 in the pandas output, and the South February cell, which has no rows, is null on both engines, not 0

Prefer to describe it?

With a provider key set up, upload the sample data, open a blank flow in the same project and ask the Flow assistant, for example: "pivot the sales so each region is a row and each month is a column." Review the proposed nodes and settings, then choose Apply and save. We ran this prompt on the sample data; see what the assistant built. Its flow can differ from the steps above in node order and names, so check each setting. The assistant never runs a flow, so click Run yourself.

Result

A new flow runs on pandas, the engine shown in the editor toolbar, and Preview always uses pandas. The run output is:

regionFebJan
North120.0130.0
Southnull80.0

pandas sorts the month columns, so Feb comes before Jan, and the null in the South row makes both columns decimal.

If you switch the toolbar engine to polars before Run, the sums and the null South Feb cell are the same but two details change:

EngineColumnsNumbers on this sample
pandasregion, Feb, Jan (sorted)decimal
polarsregion, Jan, Feb (first appearance)integer

Check the result

Save the flow, click Preview, select the Pivot Table node and click Run preview. You should see 2 rows and the North Jan cell should read 130.0. A different value means Aggregation is not sum: mean gives 65, first gives 100.

Pitfalls

  • A cell with no rows is null, not 0. This holds on both engines for every Aggregation, including sum and count: no data is not a zero total. A cell whose rows all have an empty amount does have rows, so sum and count write 0 there. To show 0 in the empty cells too, add Fill Nulls after the pivot with Strategy Zero.
  • An empty month can drop out on pandas. With mean or first, pandas leaves out a month column whose values are all empty; Polars keeps it as null. sum and count keep it on both engines.
  • Column names come from the data. A month that appears in the next file adds a column, and a downstream node that names a missing month fails.
  • Pick the aggregation on purpose. sum adds repeated rows; mean averages them, which turns North Jan into 65.
  • To go from wide back to long, use Unpivot.

Exported Python

Click Export in the editor toolbar. The pandas and polars tabs show this code for the flow. It reads sales_long.csv from the script's folder and writes sales_wide.csv. Each script keeps its engine's column order from the table above, and both leave the South Feb cell empty. The Polars script sums each region and month pair before it pivots, so a pair with no rows stays null.

python
import pandas as pd

df_sales_long = pd.read_csv('sales_long.csv')
df_sales_long = df_sales_long.pivot_table(index='region', columns='month', values='amount', aggfunc='sum').reset_index()
df_sales_long.to_csv('sales_wide.csv', index=False)

See also