DEV Community

Cover image for Create Polars DataFrames: Dict, NumPy, Pandas, pyreadstat
Muhammad Adil
Muhammad Adil

Posted on Originally published at adilaidev.com

Create Polars DataFrames: Dict, NumPy, Pandas, pyreadstat

Data comes from everywhere: a quick dict, NumPy arrays, an existing Pandas frame, or a stats file. Polars converts from all of them directly. Here is every path into a Polars DataFrame.

From a Python dict

The fastest way to hand-build a frame. Keys become column names, lists become columns.

  • import polars as pl
  • df = pl.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]})
  • # or from a list of row dicts:
  • df = pl.from_dicts([{"a": 1, "b": "x"}, {"a": 2, "b": "y"}])

From NumPy arrays

Pass a 2D array with a schema for the column names, or build columns from separate 1D arrays with a dict.

  • import numpy as np
  • arr = np.array([[1, 2], [3, 4], [5, 6]])
  • df = pl.from_numpy(arr, schema=["a", "b"])
  • # from separate 1D arrays:
  • df = pl.DataFrame({"a": np.arange(3), "b": np.random.rand(3)})

From a Pandas DataFrame

One call, and it keeps your column names and types. Use this to move a hot path from Pandas to Polars.

  • df = pl.from_pandas(pandas_df)
  • # go back when a library needs Pandas:
  • pandas_df = df.to_pandas()

From SPSS or SAS files with pyreadstat

Polars does not read .sav or .sas7bdat directly. Read them with pyreadstat into a Pandas frame first, then convert. This is how you turn a pyreadstat DataFrame into a Polars DataFrame.

  • import pyreadstat
  • pdf, meta = pyreadstat.read_sav("survey.sav") # or read_sas7bdat(...)
  • df = pl.from_pandas(pdf)

Grab a single value

To pull one scalar out of a frame, use .item. This answers getting a DataFrame item in Polars.

  • value = df.select("a").item(0, 0)
  • # or the single value of a 1x1 result:
  • total = df.select(pl.col("a").sum()).item()

From here you will want to filter rows and read and write files. The full reference lives in the Python Polars cheat sheet.


This post was originally published on my site. Read the full article and more →

Top comments (0)