// HACKER NEWS — CYBERSECURITY
Python Polars Cheatsheet (based on our O'Reilly book)
Quick reference guide for transforming, analyzing, and visualizing data with Python Polars
Polars is a library for transforming, analyzing, and visualizing data with a fast
and expressive DataFrame API.
It was first released by Ritchie Vink in 2020.
Install Polars with all of its optional dependencies from the terminal:
Import Polars in Python, and confirm which versions of Polars and its
dependencies you have installed:
Polars queries typically read data, transform it, and write the result back out.
A complete query is often a single chain of method calls:
Throughout this cheatsheet, df is a DataFrame, lf is a LazyFrame, o is a
second DataFrame to combine with df, and e stands for any expression.
So e.abs() means “call .abs() on an expression”, as in pl.col("x").abs().
Polars stores all of its data in either a Series or a DataFrame.
Unlike pandas, Polars DataFrames do not have a row index, and the API favors
immutability and method chaining over in-place modifications.
Create a Series by passing a name and a sequence of values:
Create a DataFrame from a dictionary of columns, where each value is a Series
or a plain Python sequence.
You can also use any of the pl.read_*() functions to create one from a file: