Creating DataFrames in Pandas vs Polars
Published:
Last updated:
ByJeferson Peter
2 min readPolars & Pandas
Creating a DataFrame is often the very first step in any data analysis workflow.
While Pandas is the most widely used library, Polars is becoming popular for its speed and clean syntax.
Here’s how to create DataFrames in both libraries, side by side.
From a dict of lists
# Pandas
import pandas as pd
data = {"name": ["Alice", "Bob"], "age": [25, 30]}
df_pd = pd.DataFrame(data)
print(df_pd)
# name age
# 0 Alice 25
# 1 Bob 30
# Polars
import polars as pl
data = {"name": ["Alice", "Bob"], "age": [25, 30]}
df_pl = pl.DataFrame(data)
print(df_pl)
# shape: (2, 2)
# ┌───────┬─────┐
# │ name ┆ age │
# │ --- ┆ --- │
# │ str ┆ i64 │
# ╞═══════╪═════╡
# │ Alice ┆ 25 │
# │ Bob ┆ 30 │
# └───────┴─────┘
✔ Both accept a dictionary of lists, but Polars prints with a richer table view.
From a list of dicts (records)
# Pandas
records = [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]
df_pd = pd.DataFrame(records)
print(df_pd)
# name age
# 0 Alice 25
# 1 Bob 30
# Polars
records = [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]
df_pl = pl.DataFrame(records)
print(df_pl)
# shape: (2, 2)
# ┌───────┬─────┐
# │ name ┆ age │
# │ --- ┆ --- │
# │ str ┆ i64 │
# ╞═══════╪═════╡
# │ Alice ┆ 25 │
# │ Bob ┆ 30 │
# └───────┴─────┘
✔ Very similar behavior in both libraries.
From a list of lists with column names
# Pandas
df_pd = pd.DataFrame([[1, "A"], [2, "B"]], columns=["id", "label"])
print(df_pd)
# id label
# 0 1 A
# 1 2 B
# Polars
df_pl = pl.DataFrame([[1, "A"], [2, "B"]], schema=["id", "label"])
print(df_pl)
# shape: (2, 2)
# ┌─────┬───────┐
# │ id ┆ label │
# │ --- ┆ --- │
# │ i64 ┆ str │
# ╞═════╪═══════╡
# │ 1 ┆ A │
# │ 2 ┆ B │
# └─────┴───────┘
✔ Pandas uses columns=..., Polars uses schema=....
Conclusion
- Pandas and Polars share very similar syntax for creating DataFrames.
- Polars provides a richer printout and better performance on large data.
- If you already know Pandas, moving to Polars feels very natural.
👉 Next step: in the following post, we’ll see how to select rows and columns in both libraries.