Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Migrating from pandas

pandas to Polars: The Cheat Sheet

Translate pandas code to Polars. Side-by-side examples for selecting, filtering, new columns, sorting, groupby, merge, missing values, strings, dates and apply.

If you already know pandas, you know most of what you need for Polars: the concepts (rows, columns, joins, groups) are the same, only the spelling changes. This cheat sheet translates the operations you use every day, grouped by task. Each runnable example computes the same result in both libraries so you can compare them line by line.

The runnable pandas-versus-Polars examples start with import pandas as pd and import numpy, pyarrow before import polars. In this browser runner Polars looks for those packages at import time, so the order matters here; on a normal installation any order works.

The mindset shift in one table

Idea pandas Polars
Row labels An index No index: rows are just positions
Choose columns df[["a", "b"]] df.select("a", "b")
Filter rows df[df.a > 1] df.filter(pl.col("a") > 1)
New column df["c"] = df.a * 2 df.with_columns(c=pl.col("a") * 2)
Mutate in place Yes, everywhere No: every method returns a new frame
Missing values NaN, None, NaT, pd.NA One null for every type
Order of work Eager, step by step Eager or lazy (.lazy() ... .collect())
Parallel Single-threaded mostly Multi-threaded by default

Selecting, filtering, sorting

Python Select, filter, sort side by side

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.

More one-to-one translations:

Task pandas Polars
Columns / dtypes / shape df.columns, df.dtypes, df.shape df.columns, df.schema, df.shape
Rename df.rename(columns={"a": "x"}) df.rename({"a": "x"})
Drop df.drop(columns=["a"]) df.drop("a")
Select by position df.iloc[0:3] df[0:3] or df.slice(0, 3)
One value df.at[2, "a"] df[2, "a"]
Unique rows df.drop_duplicates() df.unique()
Unique values df["a"].unique() df["a"].unique()
Value counts df["a"].value_counts() df["a"].value_counts()
Describe df.describe() df.describe()
Row count len(df) df.height or len(df)
Copy df.copy() df.clone() (rarely needed)

New columns and conditions

In pandas you assign into the frame. In Polars you describe new columns and receive a new frame.

Python New columns, np.where and apply

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.

Task pandas Polars
Conditional column np.where(cond, a, b) / df.loc[cond, "c"] = v pl.when(cond).then(a).otherwise(b)
Several branches np.select([c1, c2], [v1, v2], default) chained when/then, then otherwise
Apply a function df["a"].apply(f) pl.col("a").map_elements(f) (last resort)
Row-wise operation df.apply(f, axis=1) Use expressions: pl.sum_horizontal(...), pl.struct(...)
Clip / between s.clip(0, 1), s.between(1, 5) .clip(0, 1), .is_between(1, 5)
Cast df["a"].astype("int32") pl.col("a").cast(pl.Int32)
isin s.isin([1, 2]) pl.col("a").is_in([1, 2])
Cumulative s.cumsum() pl.col("a").cum_sum()
Shift / diff s.shift(1), s.diff() .shift(1), .diff()
Rank s.rank() .rank()

Group by, joins and reshaping

Python groupby, transform and merge

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.

(pandas shows NaN where Polars shows null for East's missing manager; see the next lesson.)

Task pandas Polars
Group and aggregate df.groupby("k").agg(...) df.group_by("k").agg(...)
Group and keep rows groupby().transform("sum") pl.col("x").sum().over("k")
First/last per group groupby("k").first() group_by("k").first() or .agg(pl.all().first())
Merge / join a.merge(b, on="k", how="left") a.join(b, on="k", how="left")
Different key names left_on=, right_on= left_on=, right_on=
Concatenate pd.concat([a, b]) pl.concat([a, b])
Pivot df.pivot_table(index=..., columns=..., values=...) df.pivot(on=..., index=..., values=...)
Melt df.melt(id_vars=...) df.unpivot(index=...)
Explode df.explode("a") df.explode("a")
Rolling mean s.rolling(3).mean() pl.col("a").rolling_mean(3)
Resample df.resample("D", on="t").sum() df.group_by_dynamic("t", every="1d").agg(...)

Missing values

Python NaN versus null

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.

Task pandas Polars
Is missing s.isna() pl.col("a").is_null()
Fill s.fillna(0) .fill_null(0), .fill_null(strategy="forward")
Drop rows df.dropna() df.drop_nulls()
Count missing df.isna().sum() df.null_count()

Strings, dates and IO

Task pandas Polars
Upper / strip s.str.upper(), s.str.strip() .str.to_uppercase(), .str.strip_chars()
Contains s.str.contains("a") .str.contains("a") (regex by default)
Replace s.str.replace("a", "b", regex=False) .str.replace("a", "b", literal=True)
Split s.str.split(",") .str.split(",") (gives a list column)
Length s.str.len() .str.len_chars()
Parse dates pd.to_datetime(s) .str.to_datetime() / .str.to_date()
Year / month s.dt.year, s.dt.month .dt.year(), .dt.month() (methods, with parentheses)
Format a date s.dt.strftime("%Y-%m") .dt.strftime("%Y-%m")
Date difference (a - b).dt.days (a - b).dt.total_days()
Read CSV pd.read_csv(path) pl.read_csv(path) / pl.scan_csv(path)
Read Parquet pd.read_parquet(path) pl.read_parquet(path) / pl.scan_parquet(path)
Write CSV df.to_csv(path, index=False) df.write_csv(path)

Searching your pandas code for .apply(, .iterrows(, inplace=True and .loc[ assignments will list almost every place where the translation needs real thought. Everything else is mostly renaming.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which Polars call is the equivalent of pandas df[df['age'] > 30]?

    filter takes a boolean expression and keeps the rows where it is true. select would return the boolean column itself.
  2. 2. What replaces pandas groupby(...).transform('sum')?

    over() computes the aggregate per group and broadcasts it to every row, like transform.
  3. 3. How do you write df['c'] = df['a'] * 2 in Polars?

    Frames are immutable in the expression API: with_columns returns a new frame with the added column.
  4. 4. What is the Polars name for pandas melt?

    melt was renamed unpivot in Polars; it turns wide columns into variable/value rows.

Summary

  • Selecting is select, filtering is filter, new columns are with_columns, grouping is group_by(...).agg(...).
  • transform becomes over, merge becomes join, melt becomes unpivot, pivot_table becomes pivot.
  • np.where becomes when/then/otherwise; apply becomes an expression whenever possible.
  • Missing data is null everywhere; NaN is only a float value.
  • No index, no in-place changes: describe results and assign them to a new name.

Next: Concepts That Differ from pandas