Skip to content

TLDWTutorials/censusapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Census Population API Data Extractor

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.


What These Scripts Do

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:

PowerShell script (get_data.ps1)

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.

Python script (get_data.py)

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)

Output

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.


How to Use the Scripts

Option A: PowerShell (Windows users)

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.


Option B: Python (macOS or Linux users — or Windows developers)

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

When to Use Each Script

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

Notes

  • 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.

About

A few scripts to extracting US Census Bureau Population Data

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published