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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file censosine21-0.1.0.tar.gz
.
File metadata
- Download URL: censosine21-0.1.0.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddc66a38f79c2f105d6aafba5ba1c33c56e19c2950527cb73b18e04e38ba68db |
|
MD5 | 63488f7331464e8ae9f9afa6f34b54af |
|
BLAKE2b-256 | a2c1ef12dfd255bcb9dc3bb29d3c2379adf21f6265c47a0a11e7b8555d26a7eb |
File details
Details for the file censosine21-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: censosine21-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96f0b72c2ce40722de8b0b4ee0f00b5e23ebc6f6c767b158d0ab9f14618c744b |
|
MD5 | 031918be86db6458e88fd64560eeeb4f |
|
BLAKE2b-256 | 2a8d6a42bd35b7c4bbd1764c391572ad6400272cc8ea37d5d4ad2cd677c58082 |