typestar

Stacking frames in Python

concat glues frames together, down the rows or across the columns.

import pandas as pd

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

print(pd.concat([july, august], ignore_index=True))
print(pd.concat([july, august], keys=["jul", "aug"]))
print(pd.concat([july, august.rename(columns={"runs": "later"})], axis=1))

How it works

  1. ignore_index=True renumbers instead of repeating labels.
  2. axis=1 aligns on the index and adds columns.
  3. keys labels which frame each block came from.

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
337
Tokens
142
Three-star pace
110 tpm

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

Type this snippet

Step 2 of 4 in Reshaping & joining, step 20 of 26 in pandas.

← Previous Next →