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
ignore_index=Truerenumbers instead of repeating labels.axis=1aligns on the index and adds columns.keyslabels which frame each block came from.
Keywords and builtins used here
asprint
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.
Step 2 of 4 in Reshaping & joining, step 20 of 26 in pandas.