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.
- 1InputFile Inputsales_long.csv
- 2TransformPivot Tableregion × month · sum of amount
- 3OutputFile Outputsales_wide
Sample data
Save this as sales_long.csv. North has two January sales, and South has no
February sale.
region,month,amount
North,Jan,100
North,Feb,120
South,Jan,80
North,Jan,30Steps
- On Datasets, upload
sales_long.csv. - In a new flow, add File Input. Set File type to CSV and Dataset
to
sales_long.csv. - 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.
- Index columns:
- Add File Output. Set File type to CSV (.csv) and Dataset name to
sales_wide. Connect the nodes in order and click Run.
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:
| region | Feb | Jan |
|---|---|---|
| North | 120.0 | 130.0 |
| South | null | 80.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:
| Engine | Columns | Numbers on this sample |
|---|---|---|
| pandas | region, Feb, Jan (sorted) | decimal |
| polars | region, 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
sumandcount: no data is not a zero total. A cell whose rows all have an emptyamountdoes have rows, sosumandcountwrite 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
meanorfirst, pandas leaves out a month column whose values are all empty; Polars keeps it as null.sumandcountkeep 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.
sumadds repeated rows;meanaverages them, which turns NorthJaninto 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.
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)