Sorting and Ranking — Pandas vs Polars
Published:
• Last updated:
• By Jeferson Peter
Polars & Pandas
Imagine you have a dataset of students with their scores and want to order them or assign ranks.
Both Pandas and Polars make this straightforward, though with slightly different syntax.
Example data
import pandas as pd
import polars as pl
data = {"name": ["Alice", "Bob", "Charlie"], "score": [85, 92, 78]}
df_pd = pd.DataFrame(data)
df_pl = pl.DataFrame(data)
Sorting
# Pandas
print(df_pd.sort_values("score"))
# name score
# 2 Charlie 78
# 0 Alice 85
# 1 Bob 92
# Polars
print(df_pl.sort("score"))
# shape: (3, 2)
# ┌─────────┬───────┐
# │ name ┆ score │
# │ --- ┆ --- │
# │ str ┆ i64 │
# ╞═════════╪═══════╡
# │ Charlie ┆ 78 │
# │ Alice ┆ 85 │
# │ Bob ┆ 92 │
# └─────────┴───────┘
Ranking
# Pandas
df_pd["rank"] = df_pd["score"].rank(ascending=False)
print(df_pd)
# name score rank
# 0 Alice 85 2.0
# 1 Bob 92 1.0
# 2 Charlie 78 3.0
# Polars
df_pl = df_pl.with_columns(pl.col("score").rank("dense", descending=True).alias("rank"))
print(df_pl)
# shape: (3, 3)
# ┌─────────┬───────┬─────┐
# │ name ┆ score ┆ rank│
# │ --- ┆ --- ┆ --- │
# │ str ┆ i64 ┆ u32 │
# ╞═════════╪═══════╪═════╡
# │ Alice ┆ 85 ┆ 2 │
# │ Bob ┆ 92 ┆ 1 │
# │ Charlie ┆ 78 ┆ 3 │
# └─────────┴───────┘
Conclusion
- Sorting:
sort_values()in Pandas,.sort()in Polars. - Ranking:
rank()in Pandas,.rank()expression in Polars. - Both provide flexible tools for ordering and ranking data.