Python package

Every table as a DataFrame, offline: the units, both censuses, the establishments and the dwellings.

Install

shell
pip install "morocco-communes[pandas]"

The package on PyPI

Use

Python
import morocco_communes as mc

communes = mc.communes()                    # 1,503 rows
communes[communes["region_code"] == "01"]   # the 146 in Tanger-Tétouan-Al Hoceima

people = mc.indicators("people")            # the 2024 census, by unit, area and sex
economy = mc.economy()                      # the establishments, by unit
housing = mc.housing()                      # the urban dwellings, by unit

Every table comes back as a pandas DataFrame. With as_frame=False it’s a list of dicts instead, and then the package needs nothing but the standard library.

Tables

CallRows
regions(), provinces(), cercles(), communes(), arrondissements()one per unit
indicators(subject, census)one per unit and area, and per sex for people
economy()one per unit, from the country down to the commune
housing()one per unit with an urban area, 784 of them
crosswalk()one per commune renumbered in 2015

indicators takes "people" or "households", and census "2024" or "2014". The 2024 census asks 73 of the 2014 questions the same way, so those columns subtract.

What each column means

  • fields() gives the column, an English label, HCP’s own heading and the unit, and for 2024 the concept HCP defines it under, with its wording. It takes "indicators", "indicators2014", "economy" or "housing".
  • sources() gives each workbook’s URL, its SHA-256 and the date it was read.
  • version is the dataset’s version, which is also the package’s.

Codes

A unit is identified by its HCP code, and the code is the hierarchy: 01.511.01.0 is a commune of province 01.511, in région 01. Codes stay strings, so code_digits keeps the leading zeros that make it 9 digits wide.

What it leaves out

Boundaries, and the area and density drawn from them, are under ODbL, so they stay in the downloads and the API. Search, the urban centres and the commune at a point are in the API too.

Licence

The code is MIT. The data is from the Haut-Commissariat au Plan (RGPH 2024 and 2014); credit it when you publish it.