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
how=verticalneeds matching schemas.vertical_relaxedcasts where the types differ.horizontalanddiagonalcover the other shapes.
Keywords and builtins used here
asprint
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.
Step 5 of 5 in Reshaping & joining, step 19 of 32 in Polars.