Merging frames in Python
merge is a SQL join: keys, a how, and suffixes when names collide.
import pandas as pd
runs = pd.DataFrame({"lang": ["python", "rust", "css"], "tpm": [104, 98, 79]})
tours = pd.DataFrame({"lang": ["python", "rust", "go"], "tours": [11, 12, 3]})
print(runs.merge(tours, on="lang"))
print(runs.merge(tours, on="lang", how="left"))
print(runs.merge(tours, on="lang", how="outer", indicator=True))
How it works
howis inner by default — sayleftwhen you mean keep everything.onnames the shared key;left_on/right_onwhen they differ.indicator=Trueshows which side each row came from.
Keywords and builtins used here
asprint
The run, in numbers
- Lines
- 8
- Characters to type
- 328
- Tokens
- 141
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 77 seconds.
Step 1 of 4 in Reshaping & joining, step 19 of 26 in pandas.