# pxwebpy
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/stefur/pxwebpy/ci.yml?style=flat-square&label=ci)](https://github.com/stefur/pxwebpy/actions/workflows/ci.yml)
![PyPI - Version](https://img.shields.io/pypi/v/pxwebpy?style=flat-square)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pxwebpy?style=flat-square)
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json&style=flat-square)](https://github.com/astral-sh/uv)
[![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json&style=flat-square)](https://github.com/astral-sh/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](https://scb.se), [Statistics Finland](https://www.stat.fi), [Statistics Greenland](https://stat.gl) and [Statistics Norway](https://www.ssb.no).
## Example usage
```python
>>> 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](examples/example.py) for more details on how to use pxwebpy.
Raw data
{
"_id": null,
"home_page": null,
"name": "pxwebpy",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "dataframes, pandas, polars, pxweb, statistics",
"author": null,
"author_email": "stefur <stefan@furne.net>",
"download_url": "https://files.pythonhosted.org/packages/88/98/4a8ebfd94450cbc5e742d727bbbfdee108be7806b59828f1d25276d4e9c4/pxwebpy-0.3.2.tar.gz",
"platform": null,
"description": "# pxwebpy\n[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/stefur/pxwebpy/ci.yml?style=flat-square&label=ci)](https://github.com/stefur/pxwebpy/actions/workflows/ci.yml)\n![PyPI - Version](https://img.shields.io/pypi/v/pxwebpy?style=flat-square)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pxwebpy?style=flat-square)\n[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json&style=flat-square)](https://github.com/astral-sh/uv)\n[![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json&style=flat-square)](https://github.com/astral-sh/ruff) \nEasily get data from the PxWeb API and into either a polars or pandas dataframe. \n \nPxwebpy parses the PxWeb table data as well as metadata using the json-stat2 response format. \n \nIt has been tested with [Statistics Sweden](https://scb.se), [Statistics Finland](https://www.stat.fi), [Statistics Greenland](https://stat.gl) and [Statistics Norway](https://www.ssb.no). \n\n## Example usage\n```python\n>>> from pxwebpy.table import PxTable\n>>> import pandas as pd\n\n# Create a table object, setting up a URL for a table\n>>> tbl = PxTable(url=\"https://api.scb.se/OV0104/v1/doris/sv/ssd/START/HE/HE0110/HE0110A/SamForvInk1\")\n\n# Check out the table variables that we can use\n>>> tbl.get_table_variables()\n{'region': ['Riket',\n 'Stockholms l\u00e4n',\n 'Upplands V\u00e4sby',\n 'Vallentuna',\n...\n 'Pite\u00e5',\n 'Haparanda',\n 'Kiruna'],\n 'k\u00f6n': ['m\u00e4n', 'kvinnor', 'totalt'],\n '\u00e5lder': ['totalt 16+ \u00e5r',\n '16-19 \u00e5r',\n 'totalt 20+ \u00e5r',\n '20-64 \u00e5r',\n '65+ \u00e5r',\n '20-24 \u00e5r',\n '25-29 \u00e5r',\n...\n '75-79 \u00e5r',\n '80-84 \u00e5r',\n '85+ \u00e5r'],\n 'inkomstklass': ['totalt',\n '0',\n '1-19 tkr',\n '20-39 tkr',\n...\n '600-799 tkr',\n '800-999 tkr',\n '1000+ tkr'],\n 'tabellinneh\u00e5ll': ['Medelinkomst, tkr',\n 'Medianinkomst, tkr',\n 'Totalsumma, mnkr',\n 'Antal personer'],\n '\u00e5r': ['1999',\n '2000',\n...\n '2021',\n '2022']}\n\n# Construct a query using a selection of variables we're interested in\n>>> tbl.create_query({\"tabellinneh\u00e5ll\": [\"Medianinkomst, tkr\"], \"\u00e5lder\": [\"totalt 16+ \u00e5r\"]})\n\n# Now we can get the data\n>>> tbl.get_data()\n\n>>> tbl\n\nPxTable(url='https://api.scb.se/OV0104/v1/doris/sv/ssd/START/HE/HE0110/HE0110A/SamForvInk1',\n query={'query': [{'code': 'ContentsCode', 'selection': {'filter': 'item', 'values': ['HE0110J8']}}, {'code': 'Alder', 'selection': {'filter': 'item', 'values': ['tot16+']}}], 'response': {'format': 'json-stat2'}},\n metadata={'label': 'Sammanr\u00e4knad f\u00f6rv\u00e4rvsinkomst, medianinkomst f\u00f6r boende i Sverige hela \u00e5ret, tkr efter \u00e5lder, tabellinneh\u00e5ll och \u00e5r', 'note': None, 'source': 'SCB', 'updated': '2024-01-12T05:52:00Z'},\n fetched=2024-06-16 10:30:34.085020,\n dataset=[{'\u00e5lder': 'totalt 16+ \u00e5r', 'tabellinneh\u00e5ll': 'Medianinkomst, tkr', '\u00e5r': '1999', 'value': 159.4}, {'\u00e5lder': 'totalt 16+ \u00e5r', 'tabellinneh\u00e5ll': 'Medianinkomst, tkr', '\u00e5r': '2000', 'value': 165.3}, ...])\n\n# Using the dataset we can then create a Pandas dataframe\n>>> df = pd.DataFrame(tbl.dataset)\n>>> print(df)\n\n \u00e5lder tabellinneh\u00e5ll \u00e5r value\n0 totalt 16+ \u00e5r Medianinkomst, tkr 1999 159.4\n1 totalt 16+ \u00e5r Medianinkomst, tkr 2000 165.3\n2 totalt 16+ \u00e5r Medianinkomst, tkr 2001 172.4\n3 totalt 16+ \u00e5r Medianinkomst, tkr 2002 179.4\n4 totalt 16+ \u00e5r Medianinkomst, tkr 2003 185.1\n5 totalt 16+ \u00e5r Medianinkomst, tkr 2004 189.4\n6 totalt 16+ \u00e5r Medianinkomst, tkr 2005 192.9\n7 totalt 16+ \u00e5r Medianinkomst, tkr 2006 198.8\n8 totalt 16+ \u00e5r Medianinkomst, tkr 2007 206.2\n9 totalt 16+ \u00e5r Medianinkomst, tkr 2008 215.1\n10 totalt 16+ \u00e5r Medianinkomst, tkr 2009 218.7\n11 totalt 16+ \u00e5r Medianinkomst, tkr 2010 219.7\n12 totalt 16+ \u00e5r Medianinkomst, tkr 2011 225.0\n13 totalt 16+ \u00e5r Medianinkomst, tkr 2012 233.7\n14 totalt 16+ \u00e5r Medianinkomst, tkr 2013 240.5\n15 totalt 16+ \u00e5r Medianinkomst, tkr 2014 244.8\n16 totalt 16+ \u00e5r Medianinkomst, tkr 2015 253.7\n17 totalt 16+ \u00e5r Medianinkomst, tkr 2016 263.9\n18 totalt 16+ \u00e5r Medianinkomst, tkr 2017 272.0\n19 totalt 16+ \u00e5r Medianinkomst, tkr 2018 279.8\n20 totalt 16+ \u00e5r Medianinkomst, tkr 2019 286.8\n21 totalt 16+ \u00e5r Medianinkomst, tkr 2020 291.9\n22 totalt 16+ \u00e5r Medianinkomst, tkr 2021 301.5\n23 totalt 16+ \u00e5r Medianinkomst, tkr 2022 316.6\n```\n\nSee [examples](examples/example.py) for more details on how to use pxwebpy.",
"bugtrack_url": null,
"license": null,
"summary": "Get data from PxWeb API easily.",
"version": "0.3.2",
"project_urls": {
"Repository": "https://github.com/stefur/pxwebpy"
},
"split_keywords": [
"dataframes",
" pandas",
" polars",
" pxweb",
" statistics"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "337bc5b6e99c94f0ffc774c867832a170750237ebf5105c19f04d240a329a9c4",
"md5": "18c66f0bba493b280c93f793e7b926c8",
"sha256": "c08a995db50baaf815ad5ded0978ae756f298c9bcf551fd7b917b4dccf884063"
},
"downloads": -1,
"filename": "pxwebpy-0.3.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "18c66f0bba493b280c93f793e7b926c8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 7402,
"upload_time": "2024-09-15T15:06:45",
"upload_time_iso_8601": "2024-09-15T15:06:45.641318Z",
"url": "https://files.pythonhosted.org/packages/33/7b/c5b6e99c94f0ffc774c867832a170750237ebf5105c19f04d240a329a9c4/pxwebpy-0.3.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "88984a8ebfd94450cbc5e742d727bbbfdee108be7806b59828f1d25276d4e9c4",
"md5": "78b8bfcd1e7ac831e33df4aef6aae18a",
"sha256": "1c7ccc9163a63a106ea01e48a8769bc8e7feacaeaba6aa69803354f5e02629e7"
},
"downloads": -1,
"filename": "pxwebpy-0.3.2.tar.gz",
"has_sig": false,
"md5_digest": "78b8bfcd1e7ac831e33df4aef6aae18a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 63032,
"upload_time": "2024-09-15T15:06:47",
"upload_time_iso_8601": "2024-09-15T15:06:47.107123Z",
"url": "https://files.pythonhosted.org/packages/88/98/4a8ebfd94450cbc5e742d727bbbfdee108be7806b59828f1d25276d4e9c4/pxwebpy-0.3.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-15 15:06:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "stefur",
"github_project": "pxwebpy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pxwebpy"
}