Git Product home page Git Product logo

scb_nb's Introduction

DEPRECATED

SCB: Access SCB data from your IPython notebook

The goal of this project is to provide a simple and uniform interface to data from Statistics Sweden (SCB, Statistiska Centralbyrån) directly from the IPython Notebook. Specifically, it is intended to be used together with pandas for iterative data analysis and manipulation.

import pandas as pd
from SCB import SCB
        
scb = SCB('http://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101H/FoddaK')
df = scb.filter(code='Tid', kind='item', values=['2014']).get()

Background

This section describes the general outline of the API provided by SCB. The original docs can be found here (Swedish).

For any given table, the URL for the table has the following format:

API-NAME/API-VERSION/LANGUAGE/DATABASE-ID/<LEVELS>/TABLE-ID

where API-NAME currently translates to http://api.scb.se/OV0104/. The current version is 1, and possible languages are sv or en, and the DATABASE-ID is ssd. The depth of the number of levels depend on the requested table. So, effectively, any given table URL will have the following structure:

http://api.scb.se/OV0104/v1/sv/ssd/<LEVELS>/TABLE-ID

It's rarely obvious where the data you're looking for is actually located. For that reason, this library provides a cursor-like object which allows you to quickly jump around between the levels of the database. Once you find the table you're looking for, you're provided with a number of filters. SCB implements a limit of 100,000 rows per request so filtering is often needed.

Quickstart

Here's a workflow example. Note that these operations are executed within the notebook environment.

Finding the right table

We start by listing all the top level categories in the database. Calling the describe method on the scb instance outputs a nicely formatted HTML table with all the categories.

from SCB import SCB
scb = SCB('http://api.scb.se/OV0104/v1/doris/sv/ssd')
scb.describe() # outputs an HTML table

We select the top level category "Arbetsmarknad" by using the go method on the instance and passing in the proper level id. We continue to do so until we reach our destination table.

scb.go('AM')
scb.go('AM0502')
scb.go('ArbOrsakSyssSNI2007')

Query the table

At this point, scb points to an actual table and we can call describe to get a list of possible filters.

scb.describe() # list possible filters
scb.filter(code='Tid', kind='item', values=['2014']) # filter the results
df = scb.get() # save the data in a pandas dataframe

The actual HTTP request is not made until we call get on the instance. Boom! From this point on df is just your ordinary pandas dataframe. Have fun!

Installation

Just clone this repository and import the SCB module.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.