Python US Census API Tutorial (Part IV) - Plot Median Age in Each State

Published: 10 September 2023
on channel: Roman's Coding Tutorials
53
5

Link to census data tables:
https://api.census.gov/data/2015/acs/...

Tutorial code:
1 import plotly.offline as plt
2 from us.states import lookup
3 from census import Census
4 import pandas as pd
5 from os import getenv
6
7 ####### Grab census data ########
8 c = Census(getenv("CENSUS_API_KEY"))
9 data = c.acs5.state("B01002_001E", Census.ALL)
10 formatted_data = []
11 for entry in data:
12 state = lookup(entry["state"]).abbr
13 pop = entry.pop("B01002_001E")
14 formatted_data.append({"state": state, "POP": pop})
15 census_dataframe = pd.DataFrame(formatted_data)
16
17 ####### Build dictionaries and plot with plotly ########
18 data = [
19 dict(
20 type="choropleth",
21 locations=census_dataframe["state"],
22 z=census_dataframe["POP"].astype(float),
23 locationmode="USA-states",
24 )
25 ]
26
27 ####### Save the plot to an HTML file #######
28 fig = dict(data=data, layout={"geo":{"scope":"usa"}})
29 plt.plot(fig)

Soundtrack:

"A Vibe Nostalgic" by Mindseye
https://freemusicarchive.org/music/Mi...
Beyond cropping a snippet from the full song, no changes were made to this content.

US Map Video Clip:
https://pixabay.com/videos/kid-bike-r...


On this page of the site you can watch the video online Python US Census API Tutorial (Part IV) - Plot Median Age in Each State with a duration of hours minute second in good quality, which was uploaded by the user Roman's Coding Tutorials 10 September 2023, share the link with friends and acquaintances, this video has already been watched 53 times on youtube and it was liked by 5 viewers. Enjoy your viewing!