Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

Your First DataFrame

Create a Polars DataFrame, inspect its shape, schema and columns, peek at rows with head, tail and glimpse, and read values back out as Python objects.

A DataFrame is a two-dimensional table: named columns, each holding values of one type, with the same number of rows in every column. In this lesson you will create one, ask it basic questions (how big is it? what types do the columns have?), look at some rows, and pull values back into plain Python.

We will reuse small tables like the ones below throughout the track, so it is worth typing them yourself and experimenting.

Creating a DataFrame from a dictionary

The most common way to build a small DataFrame is a dictionary that maps column names to lists of values.

Python Building a DataFrame

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 printed table shows the shape (rows, columns) above the grid, and under each column name the data type. Polars guessed Int64 for the ids, String for names, Float64 for amounts and Boolean for the flags. It looked at the values to decide.

Asking the DataFrame about itself

A handful of attributes tell you everything about the structure without printing the data.

Python Shape, columns and schema

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.

  • shape is a tuple (height, width).
  • columns lists names, dtypes lists types in the same order.
  • schema pairs each name with its type. It behaves like an ordered dictionary, so orders.schema["amount"] gives the type of one column.

Peeking at rows

For large tables you never print everything. These methods show a slice.

Python head, tail, sample, glimpse and describe

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.

glimpse() prints a compact vertical summary: each column name, its type and the first few values. It is the quickest way to get oriented in a wide table with dozens of columns. describe() returns another DataFrame of summary statistics, so you can filter or export it like any other.

Getting values out

Sooner or later you need plain Python again: a list for a template, a number for a report.

Python From DataFrame to Python

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.

item() returns the single value of a one-by-one DataFrame; it raises an error if the shape is anything else, which protects you from silently picking the wrong cell.

Converting to Python objects is fine for small results such as a summary, but avoid doing it for millions of rows inside loops. The whole point of Polars is to keep the work inside its fast engine and extract only the final answer.

Printing wide or long tables

By default Polars shows the first and last rows and hides the middle of long tables. A table with many columns is truncated horizontally. Display Settings shows how to change that. For now, remember that what you see in the terminal is only a rendering: the data itself is complete.

Common beginner mistakes

  1. Different lengths. Every list in the dictionary must have the same number of items, or Polars raises an error about mismatched lengths.
  2. Expecting an index. orders[0] does not mean "row with label 0". Use orders.row(0) for a row, or orders["amount"] for a column.
  3. Mixed types in one column. A column with 1, "two" and 3.0 cannot be stored. Clean the data or choose a type deliberately. You will see how in Creating DataFrames.
  4. Forgetting results are new. orders.head(2) does not change orders. Assign the result if you want to keep it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does df.shape return for a table with 5 rows and 4 columns?

    shape is a tuple (height, width): rows first, then columns.
  2. 2. Which call gives you the data types of all columns as name-to-type pairs?

    df.schema is an ordered mapping of column names to data types. df.dtypes is a list of types without names.
  3. 3. What does df.select(pl.col('amount').sum()).item() return?

    item() extracts the lone value from a 1x1 DataFrame (and raises an error otherwise).
  4. 4. Which method prints a compact vertical summary: one line per column with type and first values?

    glimpse() lists each column with its type and leading values. describe() computes summary statistics instead.

Summary

  • Build a DataFrame from a dictionary of equal-length lists; Polars infers each column type.
  • shape, columns, dtypes and schema describe the structure.
  • head, tail, sample, glimpse and describe let you inspect data without printing it all.
  • to_list, row, rows, to_dicts and item convert results back to Python.

Next: Data Types