Writing Data in Polars: write_csv, write_json, write_parquet

3D visualization of a factory sorting data into CSV boxes, JSON wraps, and Parquet cubes, representing Polars data writing methods.

You’ve read, cleaned, and analyzed your data in Polars. Now you need to save your results. If you’ve ever wondered how Polars write data when exporting your work, you’ll find its “writer” methods are just as fast and easy as its “reader” methods.

The Setup

Let’s create a final DataFrame to save.

import polars as pl
df_final = pl.DataFrame({
    "id": [1, 2, 3],
    "sales_total": [150.5, 200.0, 99.0]
})

1. write_csv()

This is the most common method for sharing data with other tools (like Excel).

# Save to a CSV file
df_final.write_csv("final_report.csv")

Pro Tip: You can control the separator: df_final.write_csv("report.tsv", separator="\t") (for a Tab-Separated file)

2. write_json()

This is great for saving data to be used by a web app or API.

# 'row_oriented=True' makes it a list of dictionaries:
# [{"id": 1, ...}, {"id": 2, ...}]
df_final.write_json("final_report.json", row_oriented=True)

3. write_parquet() (The “2026” Way)

This is the best and fastest way to save your data if you plan to read it back into Polars (or Pandas) later. It’s much smaller and 10-100x faster to read.

# You may need to 'pip install polars[pyarrow]'
df_final.write_parquet("final_report.parquet")

Rule of thumb:

  • Use write_csv() to share with non-programmers.
  • Use write_json() to share with web APIs.
  • Use write_parquet() to save your own work.

Key Takeaways

  • Polars offers fast and easy methods to save data, including write_csv(), write_json(), and write_parquet().
  • Use write_csv() for sharing data with non-programmers, as it’s compatible with tools like Excel.
  • Opt for write_json() when saving data for web applications or APIs.
  • Write_parquet() is the best choice for future reading in Polars or Pandas, as it is faster and produces smaller files.

Similar Posts

Leave a Reply