import pandas as pd
df = pd.read_csv('Zip_Zhvi_SingleFamilyResidence.csv', encoding='latin-1', dtype={"RegionName": str})
df['avg'] = df.mean(axis=1)
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()
df.head()
df['avg'] = df.mean(axis=1)
by_state = pd.DataFrame(df.groupby('State')['avg'].mean())
by_state.reset_index(inplace=True)
by_state.head()
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()