Selecting Rows and Columns — Pandas vs Polars
Published:
Last updated:
ByJeferson Peter
2 min readPolars & Pandas
Imagine you just loaded a dataset of customer information into a DataFrame.
The first thing you’ll probably want to do is explore: see only thenamecolumn, filter customers older than 25, or grab a specific row.
Both Pandas and Polars let you do this, but with slightly different syntax. Let’s compare.
Selecting a single column
# Pandas
import pandas as pd
df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [25, 30]})
print(df["name"])
# 0 Alice
# 1 Bob
# Name: name, dtype: object
# Polars
import polars as pl
df = pl.DataFrame({"name": ["Alice", "Bob"], "age": [25, 30]})
print(df.select("name"))
# shape: (2, 1)
# ┌───────┐
# │ name │
# │ --- │
# │ str │
# ╞═══════╡
# │ Alice │
# │ Bob │
# └───────┘
Selecting multiple columns
# Pandas
print(df[["name", "age"]])
# name age
# 0 Alice 25
# 1 Bob 30
# Polars
print(df.select(["name", "age"]))
# shape: (2, 2)
# ┌───────┬─────┐
# │ name ┆ age │
# │ --- ┆ --- │
# │ str ┆ i64 │
# ╞═══════╪═════╡
# │ Alice ┆ 25 │
# │ Bob ┆ 30 │
# └───────┴─────┘
Filtering rows
# Pandas
print(df[df["age"] > 25])
# name age
# 1 Bob 30
# Polars
print(df.filter(pl.col("age") > 25))
# shape: (1, 2)
# ┌───────┬─────┐
# │ name ┆ age │
# │ --- ┆ --- │
# │ str ┆ i64 │
# ╞═══════╪═════╡
# │ Bob ┆ 30 │
# └───────┴─────┘
Selecting rows by index
# Pandas
print(df.iloc[0])
# name Alice
# age 25
# Name: 0, dtype: object
⚠️ In Polars, rows are not accessed directly by index — you filter them by conditions instead.
Conclusion
- Pandas: use
loc/ilocfor rows,[]for columns. - Polars: use
.select()for columns and.filter()for rows.
👉 Next step: in the following post, we’ll compare CSV read performance between Pandas and Polars.