Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท DataFrames and Series

Selecting and Slicing

Pick columns and rows in Polars with select, drop, bracket indexing, slice, head, tail, gather and row access, and learn why select is preferred over indexing.

Selecting means "give me this part of the table": some columns, some rows, a single cell. Polars offers several ways. One of them, select with expressions, is the recommended one because it works in lazy mode and can be optimized. The others, such as bracket indexing, are shortcuts for exploring.

Python Sample data used on this page

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.

Selecting columns

Python select, drop and friends

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.

Notes:

  • select accepts plain strings, expressions, and lists of either.
  • pl.col(...) takes several names, a dtype, or a regex wrapped in ^...$.
  • drop is the inverse of select; with missing names it raises an error unless strict=False.
  • pl.all() means every column; exclude removes some of them. Column Selectors goes deeper.

Bracket indexing

Square brackets are convenient in a notebook.

Python Bracket indexing

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.

Bracket indexing is positional for rows. There are no labels. It works eagerly only; a LazyFrame does not support it. In scripts and pipelines, prefer select, filter and slice.

Slicing rows

head, tail, slice and limit take rows by position.

Python Row slices

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.

slice(offset, length) is the base operation; head(n) equals slice(0, n). All of them work in lazy mode and Polars pushes them down so that, for example, a head(5) on a lazy CSV scan reads only what is needed.

Getting one row or one value

Python Single rows and cells

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.

row(by_predicate=...) demands exactly one match, and raises an error if there are zero or several. That strictness is useful for looking up a record by a unique key.

Gathering by position

gather and Series.gather pick rows by a list of positions, in any order and with repeats. sample and top_k pick rows by chance or by value.

Python Gather, top_k and bottom_k

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.

Rows by condition

To keep rows by what they contain, you use filter rather than positions. That is the subject of the next lesson, Filtering and Sorting.

Choosing the right tool

  • Exploring in a notebook: brackets and head are fine.
  • Reusable code and lazy pipelines: select, filter, slice.
  • Whole-column math: with_columns with expressions, never loops over rows.
  • A single value for Python code: item() or row(...).

If you find yourself writing for row in df.iter_rows(): to compute something, stop and ask whether an expression can do it. Row iteration leaves the fast engine and is typically hundreds of times slower.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does df['city'] return?

    Selecting one column by name with brackets returns a Series. Use a list of names to get a DataFrame.
  2. 2. Which selects every column except 'capital'?

    pl.all() selects all columns and exclude removes some. df.drop('capital') does the same.
  3. 3. What is df.slice(4, 3)?

    slice(offset, length) starts at the offset and takes length rows.
  4. 4. Why is select preferred over bracket indexing in pipelines?

    Bracket indexing is eager-only. select and filter take expressions and also run on LazyFrames, which the optimizer can improve.

Summary

  • select, drop and pl.col(...) (names, dtypes, regex) choose columns.
  • Brackets give quick positional access in eager mode; slice, head, tail and gather take rows by position.
  • row, item and to_list extract Python values; top_k and bottom_k pick by value.
  • Prefer expression-based selection in reusable and lazy code.

Next: Filtering and Sorting