typestar

Concatenating frames in Python

concat stacks frames vertically, or glues them side by side.

import polars as pl

july = pl.DataFrame({"lang": ["python", "rust"], "runs": [12, 8]})
august = pl.DataFrame({"lang": ["python", "sql"], "runs": [15, 4]})

print(pl.concat([july, august]))
print(pl.concat([july, august]).group_by("lang").agg(pl.col("runs").sum())
      .sort("lang"))

widened = pl.DataFrame({"note": ["a", "b"]})
print(pl.concat([july, widened], how="horizontal"))

How it works

  1. how=vertical needs matching schemas.
  2. vertical_relaxed casts where the types differ.
  3. horizontal and diagonal cover the other shapes.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
377
Tokens
165
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 90 seconds.

Type this snippet

Step 5 of 5 in Reshaping & joining, step 19 of 32 in Polars.

← Previous Next →