typestar

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

  1. how is inner by default — say left when you mean keep everything.
  2. on names the shared key; left_on/right_on when they differ.
  3. indicator=True shows which side each row came from.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 4 in Reshaping & joining, step 19 of 26 in pandas.

← Previous Next →