Approach for small lookup files for postcode geographies.
Don't use NI postcodes except for statistical/internal business uses. See ONSPD source.
pip install git+https://github.com/mysociety/mini-postcode-lookup
uvx --from git+https://github.com/mysociety/mini-postcode-lookup mini-postcode-lookup tui
python -m mini_postcode_lookup get-postcode "SW1A 0AA " --area-type lsoa
Use python -m mini_postcode_lookup --help
for more.
import pandas as pd
from mini_postcode_lookup import AllowedAreaTypes, MiniPostcodeLookup, IMDInclude
df = pd.read_csv(...) # source here with a postcode column
deprivation_df = pd.read_csv("https://pages.mysociety.org/composite_uk_imd/data/uk_index/latest/UK_IMD_E.csv")[["lsoa", "UK_IMD_E_pop_decile"]]
lookup = MiniPostcodeLookup()
df["lsoa"] = lookup.get_series(df["postcode"], area_type=AllowedAreaTypes.LSOA)
#doesn't need the lsoa already present - but same approach for both.
df["imd_decile"] = lookup.get_series(df["postcode"], area_type=IMDInclude.DECILE)