Skip to main content

Python library to interact with the API of the National Institute of Statistics (INE ) of Spain that offers the data Dissemination System of the Population and Housing Censuses 2021 (SDC21).

Project description

censosine21

Python library to interact with the API of the National Institute of Statistics (INE ) of Spain that offers the data Dissemination System of the Population and Housing Censuses 2021 (SDC21)

Read this in Spanish

Installation

The censosine21 package is available as an open source library published at PyPI. You can install censosine2021 by simply executing this command:

pip install censosine21

If you already have installed censosine21, you can upgrade it using:

pip install censosine21 --upgrade

Kick off

The first step to use censosine21 is creating the CensosINE21 object.

from censosine21 import CensosINE21

hogares = CensosINE21()
hogares.post("hog", 'SHOGARES', ['ID_RESIDENCIA_N2', 'ID_TENEN_VIV' ], "ES")

At this first moment, the object's attributes have no data. Using the post() method, the library will make the request to the API according to the indicated parameters (table, metrics, variables and language) and will download the query data (metadata and data).

hogares.post("hog", 'SHOGARES', ['ID_RESIDENCIA_N2', 'ID_TENEN_VIV' ], "ES")

Objet attributes

  • table: Data set available from SDC21
  • metrics: Unit of measure
  • variables: Variables
  • language: Language used for the results
  • params: JSON of parameters to make the POST request to the API
  • api_url: official URL of the INE API
  • metadata: Metadata of the response JSON
  • data: Response JSON data

Conversion to CSV format and filters

Once the query is made, the class has the to_csv() method to save the data from the data attribute in CSV format.

hogares.to_csv('csv/nhorares_regimen_provincias.csv')

It is also possible to filter the dataset by indicating the key-value pair from the variable and the filter text using the filter() method.

hogares_filter_asturias = hogares.filter('ID_RESIDENCIA_N2', 'asturias')

This same filtering option is also available in the to_csv() method.

hogares.to_csv('csv/nhorares_regimen_asturias.csv','ID_RESIDENCIA_N2', '33 Asturias')

Show API entities info

The library has a set of static methods to access the information from the tables, units of measurement and API variables and their description in Spanish and English.

Being static methods, it is not necessary to instantiate an object to use it. We just use the class and the query method

from censosine21 import CensosINE21

CensosINE21.show_tables()
CensosINE21.show_metrics()
CensosINE21.show_variables()
CensosINE21.show_languajes()

Information about metrics and variables can be filtered by table.

CensosINE21.show_metrics('hog')
CensosINE21.show_variables('nuc')

Looking for more examples?

Get the source code from the github repository.

There are more examples in the /examples directory.

Links

You can obtain more information about how the INE API works on the INE official documentation page

License

The censosine21 library is developed by Patricio Soriano @sigdeletras under license MIT License.

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

censosine21-0.1.0.tar.gz (15.1 kB view hashes)

Uploaded Source

Built Distribution

censosine21-0.1.0-py3-none-any.whl (13.0 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