Split an address column into separate fields with pandas

Updated 28 November 2026 · parseaddr.com · plain markdown

Splitting addresses with str.split(",") breaks as soon as a row has no commas, a flat number, or a German street. This script sends each row to the parseaddr API and adds the fields as new columns. It needs pandas and requests.

Get a free key More Python

Input: orders.csv

order_id,address
1001,"flat 2, 10 downing st london sw1a 2aa"
1002,"221B BAKER STREET LONDON NW16XE"
1003,"Hauptstraße 5, 10115 Berlin, Germany"

Script

import os

import pandas as pd
import requests

session = requests.Session()
session.headers["Authorization"] = f"Bearer {os.environ['PARSEADDR_API_KEY']}"
FIELDS = ["unit", "house_number", "road", "city", "postcode", "country"]


def parse(address):
    r = session.post("https://api.parseaddr.com/v1/parse", json={"address": address}, timeout=10)
    r.raise_for_status()
    parsed = r.json()["parsed"]
    return pd.Series({f: parsed.get(f) for f in FIELDS})


df = pd.read_csv("orders.csv")
df = df.join(df["address"].apply(parse))
df.to_csv("orders_split.csv", index=False)
print(df[["order_id"] + FIELDS].to_string(index=False))

Output

order_id   unit house_number         road   city postcode country
     1001 flat 2           10   downing st london sw1a 2aa     NaN
     1002    NaN         221b baker street london   nw16xe     NaN
     1003    NaN            5  hauptstraße berlin    10115 germany

Run it with PARSEADDR_API_KEY=pa_... python split.py. It writes orders_split.csv next to the input.

Things to know

Same split in Google Sheets

The Parserator add-on for Sheets is built on usaddress, which is written for US addresses. On the 200 UK addresses in the comparison, usaddress got 0 fully right (results). This Apps Script adds a =PARSEADDR() formula that calls parseaddr instead.

  1. In the sheet, open Extensions > Apps Script, paste the code below and save.
  2. In Project Settings > Script properties, add PARSEADDR_API_KEY with your key. The key stays out of the cells.
  3. In B2 type =PARSEADDR(A2:A100). Six columns fill to the right: unit, house number, road, city, postcode, country.
// Google Sheets: Extensions > Apps Script, paste this, save.
// Project Settings > Script properties: add PARSEADDR_API_KEY = your key.
// In a cell: =PARSEADDR(A2:A100)  -> unit, house_number, road, city, postcode, country
const FIELDS = ["unit", "house_number", "road", "city", "postcode", "country"];

function PARSEADDR(input) {
  const key = PropertiesService.getScriptProperties().getProperty("PARSEADDR_API_KEY");
  const rows = Array.isArray(input) ? input.map(r => String(r[0]).trim()) : [String(input).trim()];
  const cache = CacheService.getScriptCache();
  const out = rows.map(a => (a ? JSON.parse(cache.get(a.slice(0, 200)) || "null") : FIELDS.map(() => "")));
  const todo = rows.map((a, i) => i).filter(i => out[i] === null);
  for (let s = 0; s < todo.length; s += 10) {               // free key: 10 calls a second
    if (s > 0) Utilities.sleep(1000);
    const batch = todo.slice(s, s + 10);
    const res = UrlFetchApp.fetchAll(batch.map(i => ({
      url: "https://api.parseaddr.com/v1/parse", method: "post", contentType: "application/json",
      headers: { Authorization: "Bearer " + key }, payload: JSON.stringify({ address: rows[i] }),
      muteHttpExceptions: true,
    })));
    res.forEach((r, j) => {
      const i = batch[j];
      if (r.getResponseCode() !== 200) { out[i] = ["error " + r.getResponseCode(), "", "", "", "", ""]; return; }
      const p = JSON.parse(r.getContentText()).parsed;
      out[i] = FIELDS.map(f => p[f] || "");
      cache.put(rows[i].slice(0, 200), JSON.stringify(out[i]), 21600);  // 6 hours: recalcs cost nothing
    });
  }
  return out;
}

Output for the same three rows

A: addressunithouse_numberroadcitypostcodecountry
flat 2, 10 downing st london sw1a 2aaflat 210downing stlondonsw1a 2aa
221B BAKER STREET LONDON NW16XE221bbaker streetlondonnw16xe
Hauptstraße 5, 10115 Berlin, Germany5hauptstraßeberlin10115germany

parseaddr · home · agents · pricing · llms.txt · .md
Pyke Limited, registered in England and Wales, company number 09748427.