Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท DataFrames and Series

Series Basics

A Polars Series is one typed column. Learn to create Series, use arithmetic and aggregations, inspect dtype and length, and move between Series and DataFrames.

A Series is a single named column of values, all of one type. A DataFrame is a collection of equal-length Series that share a table. You will mostly work with DataFrames and expressions, but Series appear everywhere: df["price"] returns one, expressions produce them behind the scenes, and for quick experiments a Series is the lightest way to try a method.

Creating a Series

Python Creating Series

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 first argument is the name (optional), the second the values, and dtype= fixes the type. A printed Series shows its name, length and dtype at the top, then the values.

Arithmetic and comparisons

Operations on a Series are vectorized: they act on every element at once and return a new Series.

Python Vectorized operations

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.

Comparing a Series with a value gives a Boolean Series; there is no "is this whole Series equal" surprise. To test whole-series equality use s.equals(other).

Aggregations and summaries

Python Aggregating a Series

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.

Aggregations skip nulls: the mean above uses the five real scores. len() counts all elements, count() counts non-null ones, and null_count() counts the missing ones. value_counts returns a small DataFrame of values and how often each appears.

Sorting, ranking and picking

Python Sort, rank and pick

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.

Indexing with an integer returns a Python value; slicing returns a Series. Negative indices count from the end. There is no label index; positions are all there is.

Series and DataFrame

You move between the two constantly.

Python Series to DataFrame and back

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.

df["a"] and df.get_column("a") both return a Series. Use get_column in library code because it reads more clearly. To add a Series as a column, pass it to with_columns (its name becomes the column name) or use df.insert_column(index, series).

Series versus expressions

It is easy to confuse the two:

Series Expression
What it is Actual data A recipe describing a computation
Example df["price"] pl.col("price")
Runs Immediately Inside a context (select, filter, ...)
Works with lazy mode No Yes

Prefer expressions for real pipelines: they compose, they can run lazily, and Polars can optimize and parallelize them. Use Series for small checks and for interfacing with other libraries.

Series methods by namespace

Many methods live in namespaces on the Series: s.str.* for strings, s.dt.* for dates and times, s.list.* for list columns, s.struct.* for structs. The same namespaces exist on expressions, as you will see in String Expressions and Date and Time Expressions.

Python A string Series

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. What does pl.Series('x', [1, 2, 3]) * 2 return?

    Series arithmetic is vectorized: each element is multiplied and a new Series is returned.
  2. 2. For s = [72, 85, None], what is s.len() and s.count()?

    len() counts every element (3). count() counts the non-null elements (2).
  3. 3. Which is an expression rather than a Series?

    pl.col('price') is a recipe evaluated in a context. The others hold actual data.
  4. 4. How do you turn a Series s into a one-column DataFrame?

    Series.to_frame() wraps the Series in a DataFrame whose column keeps the Series name.

Summary

  • A Series is one typed, named column; a DataFrame is a set of Series of equal length.
  • Operations are vectorized and return new Series; aggregations skip nulls.
  • len, count and null_count differ; value_counts and describe summarize quickly.
  • Series are actual data; expressions are recipes. Prefer expressions in pipelines.

Next: Creating DataFrames