Combining DataFrames in Polars: The concat Method

3D visualization of a hydraulic press fusing three separate data blocks into one tall tower, representing Polars concatenation.

We’ve used .join() to combine data based on a key (like a SQL JOIN). But what if you just want to stack two DataFrames on top of each other? In those cases, the Polars concat function provides a simple way to do this.

This is called concatenation, and in Polars, we use pl.concat().

1. Vertical Concatenation (Stacking Rows)

This is the most common use. You have two files (e.g., “January Sales” and “February Sales”) with the same columns, and you want to stack them into one big file.

import polars as pl

df_jan = pl.DataFrame({
    "product_id": [1, 2],
    "sales": [100, 150]
})

df_feb = pl.DataFrame({
    "product_id": [1, 3],
    "sales": [200, 50]
})

# Stack them vertically (default behavior)
all_sales = pl.concat([df_jan, df_feb])
print(all_sales)

Output:

shape: (4, 2)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ product_id โ”† sales โ”‚
โ”‚ ---        โ”† ---   โ”‚
โ”‚ i64        โ”† i64   โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 1          โ”† 100   โ”‚
โ”‚ 2          โ”† 150   โ”‚
โ”‚ 1          โ”† 200   โ”‚
โ”‚ 3          โ”† 50    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. Horizontal Concatenation (Adding Columns)

This is less common, but you can also use concat to add new columns, as long as the row count matches.

df1 = pl.DataFrame({"A": [1, 2]})
df2 = pl.DataFrame({"B": [3, 4], "C": [5, 6]})

# Stack them horizontally
df_wide = pl.concat([df1, df2], how="horizontal")
print(df_wide)

Output:

shape: (2, 3)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”
โ”‚ A   โ”† B   โ”† C   โ”‚
โ”‚ --- โ”† --- โ”† --- โ”‚
โ”‚ i64 โ”† i64 โ”† i64 โ”‚
โ•žโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ก
โ”‚ 1   โ”† 3   โ”† 5   โ”‚
โ”‚ 2   โ”† 4   โ”† 6   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”˜

Key Takeaways

  • To stack two DataFrames in Polars, use pl.concat() for concatenation.
  • Vertical concatenation stacks rows from multiple files with the same columns into one DataFrame.
  • Horizontal concatenation adds new columns to a DataFrame, provided that the row counts match.

Similar Posts

Leave a Reply