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
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
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")
- 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
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')
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')
Get the source code from the github repository.
There are more examples in the /examples directory.
You can obtain more information about how the INE API works on the INE official documentation page
The censosine21 library is developed by Patricio Soriano @sigdeletras under license MIT License.