This project contains two example scripts that download state‑level population data from the U.S. Census Bureau’s Population Estimates API (CHARV).
Each script demonstrates how to build a query, call the API, and save the results as a CSV file.
PowerShell is recommended for Windows users.
Python is recommended for macOS or Linux users.
Both scripts send a request to the Census API and export the results as a CSV file.
The difference is the year requested in each query:
Uses:
https://api.census.gov/data/2023/pep/charv?get=NAME,POP,MEDAGE&for=state:*&YEAR=2021
This retrieves 2021 population estimates for all states.
Uses:
https://api.census.gov/data/2023/pep/charv?get=NAME,POP,MEDAGE&for=state:*&YEAR=2023
This retrieves 2023 population estimates for all states.
Both versions keep the same variables:
- NAME (state name)
- POP (total population)
- MEDAGE (median age)
- state (FIPS code)
Both scripts create a CSV file that looks like:
NAME,POP,MEDAGE,state
Alabama,5097000,39.4,01
Alaska,733000,35.5,02
...
The PowerShell output will contain the 2021 values.
The Python output will contain the 2023 values.
PowerShell is already included on Windows and works without extra setup.
Run the script inside its folder:
.\get_data.ps1
A CSV file will be saved beside the script.
Install the requests library if needed:
pip install requests
Run the script:
python get_data.py
The script will save a CSV file called:
census_states_2023.csv
PowerShell (Windows)
- No installation required
- Easiest option for command‑line automation
- Good for quick scheduled tasks
Python (Mac/Linux/Windows)
- Cross‑platform
- Easy to modify, extend, and integrate into data workflows
- Preferred for bulk data work or dashboards
- These scripts do not require an API key.
- The CHARV dataset includes detailed demographic breakdowns, but here we only extract the total population and median age.
- You can modify the query parameters to retrieve different years or variables.