In [3]:
import pandas as pd
df = pd.read_csv('Zip_Zhvi_SingleFamilyResidence.csv', encoding='latin-1', dtype={"RegionName": str})
df['avg'] = df.mean(axis=1)
In [4]:
from urllib.request import urlopen
import json
import plotly.express as px

with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

fig = px.choropleth(df, geojson=counties, locations='RegionName', color=df['avg'],
                           color_continuous_scale="Viridis",
                           range_color=(0, 1000000),
                           scope="usa",
                           labels={'unemp':'unemployment rate'}
                          )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
In [11]:
df.head()
Out[11]:
RegionID RegionName City State Metro CountyName SizeRank 1996-04 1996-05 1996-06 ... 2019-04 2019-05 2019-06 2019-07 2019-08 2019-09 2019-10 2019-11 2019-12 avg
0 61639 10025 New York NY New York-Newark-Jersey City New York County 1 NaN NaN NaN ... 1386270 1394397 1404225 1406599 1399918 1380178 1358401 1350481 1345845 1.495773e+06
1 84654 60657 Chicago IL Chicago-Naperville-Elgin Cook County 2 355664.0 354736.0 355404.0 ... 967557 965155 960225 956709 953095 950684 948136 946838 945928 7.130792e+05
2 61637 10023 New York NY New York-Newark-Jersey City New York County 3 NaN NaN NaN ... 1526676 1525174 1516721 1504180 1492086 1480934 1468075 1454870 1438313 1.382940e+06
3 91982 77494 Katy TX Houston-The Woodlands-Sugar Land Harris County 4 197907.0 196854.0 195911.0 ... 335035 334542 334176 334363 334127 334458 334460 334679 334309 2.619563e+05
4 84616 60614 Chicago IL Chicago-Naperville-Elgin Cook County 5 537402.0 536919.0 539044.0 ... 1203423 1204840 1199747 1194591 1188702 1183500 1179024 1175407 1174008 9.860006e+05

5 rows × 293 columns

In [12]:
df['avg'] = df.mean(axis=1)
by_state = pd.DataFrame(df.groupby('State')['avg'].mean())
by_state.reset_index(inplace=True)
by_state.head()
Out[12]:
State avg
0 AK 219656.996262
1 AL 106281.749229
2 AR 77504.733709
3 AZ 188098.425818
4 CA 398383.806727
In [ ]:
import plotly
import plotly.graph_objects as go

fig = go.Figure(data=go.Choropleth(
    locations=by_state['State'], 
    z = by_state['avg'].astype(float), 
    locationmode = 'USA-states', 
    colorscale = 'Reds',
    colorbar_title = "Average Home Price",
))

fig.update_layout(
    title_text = 'Average Home Price by State',
    geo_scope='usa',
)

fig.show()
In [ ]: