Skip to main content

Get data from PxWeb API easily.

Project description

pxwebpy

GitHub Actions Workflow Status PyPI - Version PyPI - Python Version uv ruff
Easily get data from the PxWeb API and into either a polars or pandas dataframe.

Pxwebpy parses the PxWeb table data as well as metadata using the json-stat2 response format.

It has been tested with Statistics Sweden, Statistics Finland, Statistics Greenland and Statistics Norway.

Example usage

>>> from pxwebpy.table import PxTable
>>> import pandas as pd

# Create a table object, setting up a URL for a table
>>> tbl = PxTable(url="https://api.scb.se/OV0104/v1/doris/sv/ssd/START/HE/HE0110/HE0110A/SamForvInk1")

# Check out the table variables that we can use
>>> tbl.get_table_variables()
{'region': ['Riket',
  'Stockholms län',
  'Upplands Väsby',
  'Vallentuna',
...
  'Piteå',
  'Haparanda',
  'Kiruna'],
 'kön': ['män', 'kvinnor', 'totalt'],
 'ålder': ['totalt 16+ år',
  '16-19 år',
  'totalt 20+ år',
  '20-64 år',
  '65+ år',
  '20-24 år',
  '25-29 år',
...
  '75-79 år',
  '80-84 år',
  '85+ år'],
 'inkomstklass': ['totalt',
  '0',
  '1-19 tkr',
  '20-39 tkr',
...
  '600-799 tkr',
  '800-999 tkr',
  '1000+ tkr'],
 'tabellinnehåll': ['Medelinkomst, tkr',
  'Medianinkomst, tkr',
  'Totalsumma, mnkr',
  'Antal personer'],
 'år': ['1999',
  '2000',
...
  '2021',
  '2022']}

# Construct a query using a selection of variables we're interested in
>>> tbl.create_query({"tabellinnehåll": ["Medianinkomst, tkr"], "ålder": ["totalt 16+ år"]})

# Now we can get the data
>>> tbl.get_data()

>>> tbl

PxTable(url='https://api.scb.se/OV0104/v1/doris/sv/ssd/START/HE/HE0110/HE0110A/SamForvInk1',
        query={'query': [{'code': 'ContentsCode', 'selection': {'filter': 'item', 'values': ['HE0110J8']}}, {'code': 'Alder', 'selection': {'filter': 'item', 'values': ['tot16+']}}], 'response': {'format': 'json-stat2'}},
        metadata={'label': 'Sammanräknad förvärvsinkomst, medianinkomst för boende i Sverige hela året, tkr efter ålder, tabellinnehåll och år', 'note': None, 'source': 'SCB', 'updated': '2024-01-12T05:52:00Z'},
        fetched=2024-06-16 10:30:34.085020,
        dataset=[{'ålder': 'totalt 16+ år', 'tabellinnehåll': 'Medianinkomst, tkr', 'år': '1999', 'value': 159.4}, {'ålder': 'totalt 16+ år', 'tabellinnehåll': 'Medianinkomst, tkr', 'år': '2000', 'value': 165.3}, ...])

# Using the dataset we can then create a Pandas dataframe
>>> df = pd.DataFrame(tbl.dataset)
>>> print(df)

          ålder      tabellinnehåll    år  value
0   totalt 16+ år  Medianinkomst, tkr  1999  159.4
1   totalt 16+ år  Medianinkomst, tkr  2000  165.3
2   totalt 16+ år  Medianinkomst, tkr  2001  172.4
3   totalt 16+ år  Medianinkomst, tkr  2002  179.4
4   totalt 16+ år  Medianinkomst, tkr  2003  185.1
5   totalt 16+ år  Medianinkomst, tkr  2004  189.4
6   totalt 16+ år  Medianinkomst, tkr  2005  192.9
7   totalt 16+ år  Medianinkomst, tkr  2006  198.8
8   totalt 16+ år  Medianinkomst, tkr  2007  206.2
9   totalt 16+ år  Medianinkomst, tkr  2008  215.1
10  totalt 16+ år  Medianinkomst, tkr  2009  218.7
11  totalt 16+ år  Medianinkomst, tkr  2010  219.7
12  totalt 16+ år  Medianinkomst, tkr  2011  225.0
13  totalt 16+ år  Medianinkomst, tkr  2012  233.7
14  totalt 16+ år  Medianinkomst, tkr  2013  240.5
15  totalt 16+ år  Medianinkomst, tkr  2014  244.8
16  totalt 16+ år  Medianinkomst, tkr  2015  253.7
17  totalt 16+ år  Medianinkomst, tkr  2016  263.9
18  totalt 16+ år  Medianinkomst, tkr  2017  272.0
19  totalt 16+ år  Medianinkomst, tkr  2018  279.8
20  totalt 16+ år  Medianinkomst, tkr  2019  286.8
21  totalt 16+ år  Medianinkomst, tkr  2020  291.9
22  totalt 16+ år  Medianinkomst, tkr  2021  301.5
23  totalt 16+ år  Medianinkomst, tkr  2022  316.6

See examples for more details on how to use pxwebpy.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pxwebpy-0.3.2.tar.gz (63.0 kB view details)

Uploaded Source

Built Distribution

pxwebpy-0.3.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pxwebpy-0.3.2.tar.gz.

File metadata

  • Download URL: pxwebpy-0.3.2.tar.gz
  • Upload date:
  • Size: 63.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for pxwebpy-0.3.2.tar.gz
Algorithm Hash digest
SHA256 1c7ccc9163a63a106ea01e48a8769bc8e7feacaeaba6aa69803354f5e02629e7
MD5 78b8bfcd1e7ac831e33df4aef6aae18a
BLAKE2b-256 88984a8ebfd94450cbc5e742d727bbbfdee108be7806b59828f1d25276d4e9c4

See more details on using hashes here.

File details

Details for the file pxwebpy-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: pxwebpy-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for pxwebpy-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c08a995db50baaf815ad5ded0978ae756f298c9bcf551fd7b917b4dccf884063
MD5 18c66f0bba493b280c93f793e7b926c8
BLAKE2b-256 337bc5b6e99c94f0ffc774c867832a170750237ebf5105c19f04d240a329a9c4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page