To extract statistical data from INEGI (National Institute of Statistics and Geography in Mexico) using Python, we need to first identify the specific data we are interested in and find the corresponding URL of the data source. Once we have the URL, we can use Python's web-scraping library BeautifulSoup
to extract the data and pandas
library to clean, manipulate, and analyze the data.
Here's an example code to extract population data from INEGI's website:
main.py966 chars30 lines
In this example, we use the URL of the population data for Mexican states in 2021, send a GET request to the webpage, and use BeautifulSoup to extract the table containing the population data. We then convert the table into a pandas dataframe, clean and manipulate the data by removing unnecessary rows and columns, renaming columns, and converting the population column from string to integer after removing commas. Finally, we can analyze the data using pandas functions such as sum
and mean
.
gistlibby LogSnag