Python Polars: How to Append or Concatenate DataFrames in Polars in Python
Polars is a high-performance DataFrame library built in Rust, designed for efficient data processing in Python. One of the most common tasks when working with multiple datasets is combining them, either by stacking rows vertically or by placing columns side by side horizontally. Polars provides several optimized methods for both scenarios, each suited to different use cases and schema requirements.
This guide covers every major approach to appending and concatenating DataFrames in Polars, including how to handle mismatched schemas gracefully.
Vertical Concatenation with pl.concat()
The most common way to combine DataFrames in Polars is pl.concat(), which stacks DataFrames on top of each other by default. Both DataFrames must share the same column names and data types:
import polars as pl
df1 = pl.DataFrame({"user": ["Alice"], "score": [95]})
df2 = pl.DataFrame({"user": ["Bob"], "score": [88]})
combined = pl.concat([df1, df2])
print(combined)
Output:
shape: (2, 2)
┌───────┬───────┐
│ user ┆ score │
│ --- ┆ --- │
│ str ┆ i64 │
╞═══════╪═══════╡