Skip to content

Blog

Build a Polars Pipeline Without Writing Polars

Build a visual Polars pipeline in Ciaren, export the Python script, and check sales by country against two sample CSVs, including duplicate order rows.

About this post

Published
Reading time
5 min
Author
Ciaren team

Topics: polars, visual-etl, tutorial, python-data-pipeline

Mexico leads this sales report with 100,606.16 across just five completed order rows. Is that a large sale or a join that counted the same amount twice? We'll build a seven-node flow in Ciaren, follow one order through the join, and run the exported Polars script against the same two CSVs.

Get the two inputs

Download orders.csv and customers.csv into the same folder. They contain all the rows from Ciaren 0.3.1's demo orders and customers, with only the columns used here.

FileRowsColumns used
orders.csv123order_id, customer_id, amount, status
customers.csv60id, country

For this report, sum the amount on every input row marked completed and count those rows by customer country. Each customer has a unique id. The demo deliberately includes duplicate order rows. Keep them for now so the output reconciles to the input file.

Build the flow in Ciaren

Open a project, upload both CSVs on Datasets, and create a flow in the same project. If you're starting fresh, the installation guide shows how to start the local editor.

  1. Add two File Input nodes. Select orders.csv on one and customers.csv on the other.
  2. Connect the orders input to Filter Rows. Set status equal to the text completed. That leaves 52 rows.
  3. Connect the customers input to String Transform. Select country and the upper operation. The source has usa, Usa, and USA; all three now group under USA. Uppercasing alone would leave a spelling such as U.S.A. in a separate group.
  4. Connect the cleaned customers to the left input of Join / Merge and the filtered orders to the right. Choose an inner join with id as the left key and customer_id as the right key. All 52 completed rows have one matching customer in these files.
  5. Add Group By Aggregate after the join. Group by country, sum amount, and count order_id. Every order row here has an ID, so counting non-null IDs gives the row count.
  6. Connect File Output, choose CSV, and set the dataset name to sales_by_country. Save the flow. To inspect it, select the output node, open Preview in the toolbar, and click Run preview in the panel. The preview reads the last saved version.

File Output preview with four country rows and the columns country, amount, and order_id.

The editor's preview uses pandas. To run the flow with Polars, select polars beside Run, run it, and open the run detail. Select Group By Aggregate to see the four country rows. You can also watch the flow being built (GIF).

Polars run showing 123 order rows, 52 after the filter, 52 after the join, and four after grouping.

Check the result

The export keeps the names amount and order_id for the sum and count. Here are its values, listed alphabetically for comparison.

CountrySum of amountCount of order_id
CANADA1,676.1518
MEXICO100,606.165
UK1,079.5514
USA1,403.3715
  • Trace the largest sale. Order 1011 contributes 99,999.99. Its customer, 15, appears once in the customer file, under mexico. The other four Mexican rows add 606.17. That accounts for the full Mexican total.
  • Reconcile the count. The four counts add up to 52, and the four amounts add up to 104,765.23, the sum of the completed input rows. Orders 1003 and 1008 each occur twice in the source, so those 52 rows represent 50 distinct orders. If the report calls for one sale per order, remove the exact duplicate records before summing. Changing only the count would leave their amounts counted twice.

With your own files, check the keys as well as the totals. An unmatched order disappears in an inner join, while two customer rows with the same key can multiply its amount. The join walkthrough shows how to trace that second failure.

Run the exported Python

In the editor, choose Export, select the polars tab, and copy the script. The recorded demo shows .group_by('country') in the dialog. Ciaren 0.3.1 generates .group_by('country', maintain_order=True) for the same flow, as shown below. That changes the order in which groups can appear and preserves the totals.

Generated code dialog with the eager polars tab selected and the Copy button beside the script.

python
import polars as pl

df_orders = pl.read_csv('orders.csv')
df_customers = pl.read_csv('customers.csv')
df_orders = df_orders.filter(pl.col('status') == 'completed')
df_customers = df_customers.with_columns(pl.col('country').cast(pl.Utf8).str.to_uppercase())

df_1 = (
    df_customers.join(df_orders, left_on='id', right_on='customer_id', suffix='_y')
    .group_by('country', maintain_order=True)
    .agg(pl.col('amount').sum(), pl.col('order_id').count())
)

df_1.write_csv('sales_by_country.csv')

Save it as sales.py beside the two CSVs. From that folder, run:

bash
python -m pip install polars==1.41.2
python sales.py

Open sales_by_country.csv and compare each row by country with the table above. The generated group_by(..., maintain_order=True) keeps the groups in first-seen order. It doesn't rank them by sales.

If you were writing the Polars by hand

For a report you share with a colleague, it helps to name the two measures and put the largest total first. This version uses the same inputs and writes a second CSV so you can compare them.

python
import polars as pl

orders = pl.read_csv("orders.csv").filter(pl.col("status") == "completed")
customers = pl.read_csv("customers.csv").with_columns(
    pl.col("country").str.to_uppercase()
)

report = (
    customers.join(orders, left_on="id", right_on="customer_id", how="inner")
    .group_by("country")
    .agg(
        pl.col("amount").sum().alias("total_sales"),
        pl.col("order_id").count().alias("order_rows"),
    )
    .sort("total_sales", descending=True)
)
report.write_csv("sales_by_country_ranked.csv")

Mexico comes first in the ranked file. Its 100,606.16 total and five rows match the visual run and the exported script. If a reviewer asks where that total came from, the join keys and the sum are both visible in the Python file, and the two input CSVs are enough to reproduce it.

Keep reading