Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 37 ยท DataFrames and Series

Creating DataFrames

Build Polars DataFrames from dictionaries, lists of rows, records, NumPy arrays and pandas, control orientation and schema, and handle mixed-type errors.

Real projects start by reading files, but you also create DataFrames from Python data all the time: in tests, in small lookup tables, when you receive JSON from an API, or when you convert from NumPy or pandas. Polars accepts many shapes of input and infers the right layout, and when it guesses wrong you can steer it with schema, orient and strict.

From a dictionary of columns

The usual form: keys are column names, values are equal-length sequences (lists, ranges, tuples, Series, NumPy arrays).

Python Dict of columns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

From a list of rows

A list of dictionaries is common when data comes from JSON. A list of tuples works too, but then you must provide the column names.

Python Rows as dicts or tuples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For a list of tuples, orient="row" says each tuple is a row. Without it, Polars may treat each tuple as a column. Giving an explicit orientation removes the guess. A list of dictionaries infers the schema from the first records (the infer_schema_length argument controls how many); a key that first appears later may be missed, so raise infer_schema_length or supply a schema when keys vary.

Controlling the schema

Python schema and schema_overrides

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A schema may be a dict (name to type), a list of names, or a list of (name, type) pairs. An empty DataFrame with a schema has zero rows but the right columns, which is exactly what you want as a starting point for results you will append to or join with.

From NumPy arrays

A 2-D NumPy array becomes a frame whose columns you name with schema.

Python From NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

By default a 2-D array is read column by column if orient is unspecified and the shape suggests it; pass orient="row" or orient="col" when the shape is ambiguous (for example a square array). to_numpy() converts back; a frame with mixed types becomes an array of objects, so convert numeric columns individually when you want speed.

From and to pandas

Polars converts to and from pandas in one call. This is the bridge when you use a library that expects pandas, such as a plotting or machine-learning package.

Python pandas interoperability

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The four lines marked "browser-only" are needed only in the in-page runner, where Polars may have been imported before NumPy and pandas finished loading; on your own machine you just write the imports.

This example uses numeric columns only. On a normal install, converting text or nullable columns needs the pyarrow package (pip install "polars[pandas]"). Converting copies data, so do it at the edges of your program, not inside loops.

Other constructors

Need Use
A column of consecutive integers pl.int_range(0, n, eager=True)
A repeated value pl.repeat("x", 3, eager=True)
One Series to a DataFrame series.to_frame()
From JSON text pl.read_json(io.StringIO(text))
Combine frames vertically pl.concat([df1, df2])
Combine frames side by side pl.concat([df1, df2], how="horizontal")
Python concat and ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

pl.concat with the default how="vertical" requires identical columns and types. Use how="diagonal" to allow different columns (missing ones become null) or how="vertical_relaxed" to coerce compatible types.

When creation goes wrong

  • "Could not append value of type str to a column of type i64": a column mixes types. Fix the data, or pass strict=False so Polars coerces where it can, or give an explicit schema.
  • Columns of different lengths: every column must have the same number of values (one scalar is not broadcast in a dict constructor, except when all others share one length and Polars can broadcast a single value).
  • Wrong orientation of tuples: add orient="row".
Python Handling a mixed column

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You have a list of tuples [(1, 'Ann'), (2, 'Bob')]. How do you make a DataFrame with columns id and name?

    Tuples carry no names, so give the schema, and set orient='row' so each tuple is read as a row.
  2. 2. What does pl.DataFrame(schema={'id': pl.Int64, 'label': pl.String}) create?

    A schema without data yields an empty DataFrame with the declared columns, useful as a starting point.
  3. 3. Which concat option allows frames with different columns, filling missing ones with null?

    Diagonal concatenation stacks rows and fills columns that are absent from some frames with null.
  4. 4. Why raise infer_schema_length when building from a list of dicts?

    Polars infers the schema from the first records only. A later new key or type may need more records examined.

Summary

  • Build frames from dicts of columns, lists of dicts, lists of tuples (with orient="row"), NumPy arrays or pandas.
  • Control types with schema= and schema_overrides=; use strict=False for mixed data.
  • pl.concat stacks (vertical, diagonal) or joins side by side (horizontal).
  • Convert to and from pandas and NumPy at the edges of your program.

Next: Selecting and Slicing