Skip to main content

Get data from PxWeb API easily.

Project description

pxwebpy

ci PyPI - Version PyPI - Python Version Rye 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 import PxWeb
>>> import pandas as pd

>>> URL = "https://api.scb.se/OV0104/v1/doris/sv/ssd/START/HE/HE0110/HE0110A/SamForvInk1"

>>> QUERY = {
            "query": [
                {
                "code": "Tid",
                "selection": {
                    "filter": "item",
                    "values": [
                    "2021"
                    ]
                }
                }
            ],
            "response": {
                "format": "json-stat2"
            }
        }

>>> tbl = PxWeb(URL, QUERY)
>>> print(tbl)

PxWeb(url='https://api.scb.se/OV0104/v1/doris/sv/ssd/START/HE/HE0110/HE0110A/SamForvInk1',
    query={'query': [{'code': 'Tid', 'selection': {'filter': 'item', 'values': ['2021']}}], 'response': {'format': 'json-stat2'}},
    metadata={'label': 'Sammanräknad förvärvsinkomst för boende i Sverige hela året efter ålder, tabellinnehåll och år', 'source': 'SCB', 'updated': '2023-01-10T10:42:00Z'},
    last_refresh=2023-10-29 14:21:57.628639,
    dataset=[{'ålder': 'totalt 16+ år', 'tabellinnehåll': 'Medelinkomst, tkr', 'år': '2021' ...

>>> df = pd.DataFrame(tbl.dataset)
>>> print(df)
            ålder      tabellinnehåll    år      value
0   totalt 16+ år   Medelinkomst, tkr  2021      331.5
1   totalt 16+ år  Medianinkomst, tkr  2021      301.5
2   totalt 16+ år    Totalsumma, mnkr  2021  2779588.9
3   totalt 16+ år      Antal personer  2021  8383640.0
4        16-19 år   Medelinkomst, tkr  2021       28.1
..            ...                 ...   ...        ...
71       80-84 år      Antal personer  2021   290684.0
72         85+ år   Medelinkomst, tkr  2021      214.4
73         85+ år  Medianinkomst, tkr  2021      200.1
74         85+ år    Totalsumma, mnkr  2021    57529.3
75         85+ år      Antal personer  2021   268320.0

[76 rows x 4 columns]

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.2.0.tar.gz (16.1 kB view hashes)

Uploaded Source

Built Distribution

pxwebpy-0.2.0-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

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