Skip to main content

A Python API wrapper to Statistics Denmark's DataBank API

Project description

denstatbank

denstatbank

A python wrapper to Statistics Denmark's Databank API. The package allows you to easily gather and analyse data on a variety of topics made available by Statistics Denmark.

The package provides a simple interface for professional statisticians, academics, policymakers, students, journalists and anyone interested in quantitative facts about Denmark.

Installation

The package is listed on pypi - the python package index, and can be installed with pip as:

pip install --upgrade denstatbank

Usage

A walkthrough example

Let us walkthrough a quick example of how to query for data on a specific topic. The first step is to instantiate the client. This is easily done with the following two lines of code.

>>> from denstatbank import StatBankClient
>>> sbc = StatBankClient(lang='en')

Now, let's find a table to get data from the databank. The tables method provides a list of all tables containing data currently available in the databank. Let's take a look at the first table.

>>> tdf = sbc.tables()
>>> tdf.iloc[0]
id                                                  FOLK1A
text            Population at the first day of the quarter
unit                                                number
updated                                2020-02-11T08:00:00
firstPeriod                                         2008Q1
latestPeriod                                        2020Q1
active                                                True
variables         [region, sex, age, marital status, time]

All data tables have values associated with certain variables specific to the table. The population table that we shall look at has five such variables with the names you see above. The variables themselves have a list of valid values. One quick way of finding acceptable values for the variables is by using the tableinfo method as follows:

>>> vdf = sbc.tableinfo('folk1a', variables_df=True)
>>> years = vdf[vdf['variable']=='time']['id'].tolist()

We have now extracted the list of all acceptable values for the variable 'time'. Now, we need to put this inside a dictionary where the dictionary key is the variable name (in Danish, so 'time' becomes 'tid'). The client has a method called variable_dict() which does this for you.

>>> tid = sbc.variable_dict(code='tid', values=years)

Finally, we query the data by passing the table id and the variables dictionary as a keyword arguments. The dictionary must be placed inside a list as the method can accept more than one variables dictionary.

>>> df = sbc.data(table_id='folk1a', variables=[tid])
>>> df.head()
          Population at the first day of the quarter by Indhold and time
tid                                                                   
2008Q1                                            5475791             
2008Q2                                            5482266             
2008Q3                                            5489022             
2008Q4                                            5505995             
2009Q1                                            5511451

And there we have the population data. Let us quickly plot it to get a feel for the data.

>>> df.plot(style='o-', figsize=(10, 6))

denstatbank uses the pandas python library to facilitate the handling of data. Pandas is a fast, popular and powerful library used for data analysis and manipulation. It is therefore well suited to be used with this package. There are plenty of resources available to learn from if you are new to pandas. I would highly recommend this book by the creator of the pandas himself.

Documentation

The detailed package documentation can be found here.

The official Databank API documentation can be found here.

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

denstatbank-0.8.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

denstatbank-0.8.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file denstatbank-0.8.1.tar.gz.

File metadata

  • Download URL: denstatbank-0.8.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.1

File hashes

Hashes for denstatbank-0.8.1.tar.gz
Algorithm Hash digest
SHA256 a3e9a8d15506929be45312b32f480297b5c64ec4ab71439e643e95206fd58222
MD5 177d803054c9fa8209f710b110b721cc
BLAKE2b-256 d41f1e9618f266f52125352287fd0750f81ace11489bbfd9812a53f963f3a8c6

See more details on using hashes here.

File details

Details for the file denstatbank-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: denstatbank-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.1

File hashes

Hashes for denstatbank-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f006e80b623f99f103fc78b1052baa5c70b51ca94bf0e98d398c7577a8576181
MD5 17445a22038c1fa534f269e18cfdef24
BLAKE2b-256 27e47c64b3636314942fe2a5cd6df3e1e861898ff676965d8ace987532817d2b

See more details on using hashes here.

Supported by

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