polars.concat#

polars.concat(items: Iterable[DataFrame], rechunk: bool = True, how: ConcatMethod = 'vertical', parallel: bool = True) DataFrame[source]#
polars.concat(items: Iterable[Series], rechunk: bool = True, how: ConcatMethod = 'vertical', parallel: bool = True) Series
polars.concat(items: Iterable[LazyFrame], rechunk: bool = True, how: ConcatMethod = 'vertical', parallel: bool = True) LazyFrame
polars.concat(items: Iterable[Expr], rechunk: bool = True, how: ConcatMethod = 'vertical', parallel: bool = True) Expr

Aggregate multiple Dataframes/Series to a single DataFrame/Series.

Parameters:
items

DataFrames/Series/LazyFrames to concatenate.

rechunk

Make sure that all data is in contiguous memory.

how{‘vertical’, ‘diagonal’, ‘horizontal’}

Lazy only supports the ‘vertical’ strategy.

  • Vertical: applies multiple vstack operations.

  • Diagonal: finds a union between the column schemas and fills missing column

    values with null.

  • Horizontal: stacks Series horizontally and fills with nulls if the lengths

    don’t match.

parallel

Only relevant for LazyFrames. This determines if the concatenated lazy computations may be executed in parallel.

Examples

>>> df1 = pl.DataFrame({"a": [1], "b": [3]})
>>> df2 = pl.DataFrame({"a": [2], "b": [4]})
>>> pl.concat([df1, df2])
shape: (2, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 3   │
│ 2   ┆ 4   │
└─────┴─────┘
>>> df_h1 = pl.DataFrame(
...     {
...         "l1": [1, 2],
...         "l2": [3, 4],
...     }
... )
>>> df_h2 = pl.DataFrame(
...     {
...         "r1": [5, 6],
...         "r2": [7, 8],
...         "r3": [9, 10],
...     }
... )
>>> pl.concat(
...     [
...         df_h1,
...         df_h2,
...     ],
...     how="horizontal",
... )
shape: (2, 5)
┌─────┬─────┬─────┬─────┬─────┐
│ l1  ┆ l2  ┆ r1  ┆ r2  ┆ r3  │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╪═════╡
│ 1   ┆ 3   ┆ 5   ┆ 7   ┆ 9   │
│ 2   ┆ 4   ┆ 6   ┆ 8   ┆ 10  │
└─────┴─────┴─────┴─────┴─────┘
>>> df_d1 = pl.DataFrame(
...     {
...         "a": [1],
...         "b": [3],
...     }
... )
>>> df_d2 = pl.DataFrame(
...     {
...         "a": [2],
...         "d": [4],
...     }
... )
>>> pl.concat(
...     [
...         df_d1,
...         df_d2,
...     ],
...     how="diagonal",
... )
shape: (2, 3)
┌─────┬──────┬──────┐
│ a   ┆ b    ┆ d    │
│ --- ┆ ---  ┆ ---  │
│ i64 ┆ i64  ┆ i64  │
╞═════╪══════╪══════╡
│ 1   ┆ 3    ┆ null │
│ 2   ┆ null ┆ 4    │
└─────┴──────┴──────┘