Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
# Import necessary libraries
|
2 |
import pandas as pd
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
|
5 |
# Create a dictionary of states with their region, population, and area
|
@@ -56,6 +57,7 @@ states = {
|
|
56 |
'Wyoming': {'region': 'West', 'population': 578759, 'area': 253335}
|
57 |
}
|
58 |
|
|
|
59 |
# Create a function to calculate population density
|
60 |
def calculate_density(population, area):
|
61 |
return population / area
|
@@ -63,7 +65,7 @@ def calculate_density(population, area):
|
|
63 |
# Create a function to plot the graph
|
64 |
def plot_graph(df, region):
|
65 |
plt.figure(figsize=(10, 5))
|
66 |
-
plt.bar(df
|
67 |
plt.title(f'Population Density of States in {region} Region')
|
68 |
plt.xlabel('State')
|
69 |
plt.ylabel('Population Density')
|
@@ -83,16 +85,24 @@ for state, data in states.items():
|
|
83 |
regions[region]['area'] += area
|
84 |
regions[region]['states'].append(state)
|
85 |
|
86 |
-
# Calculate the population density for each state in each region
|
|
|
87 |
for region, data in regions.items():
|
88 |
population = data['population']
|
89 |
area = data['area']
|
90 |
states = data['states']
|
91 |
-
|
92 |
for state in states:
|
93 |
-
|
94 |
-
|
|
|
95 |
state_density = calculate_density(state_population, state_area)
|
96 |
-
|
97 |
-
df = pd.DataFrame({'State': states, 'Population Density':
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Import necessary libraries
|
2 |
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
import matplotlib.pyplot as plt
|
5 |
|
6 |
# Create a dictionary of states with their region, population, and area
|
|
|
57 |
'Wyoming': {'region': 'West', 'population': 578759, 'area': 253335}
|
58 |
}
|
59 |
|
60 |
+
|
61 |
# Create a function to calculate population density
|
62 |
def calculate_density(population, area):
|
63 |
return population / area
|
|
|
65 |
# Create a function to plot the graph
|
66 |
def plot_graph(df, region):
|
67 |
plt.figure(figsize=(10, 5))
|
68 |
+
plt.bar(df['State'], df['Population Density'])
|
69 |
plt.title(f'Population Density of States in {region} Region')
|
70 |
plt.xlabel('State')
|
71 |
plt.ylabel('Population Density')
|
|
|
85 |
regions[region]['area'] += area
|
86 |
regions[region]['states'].append(state)
|
87 |
|
88 |
+
# Calculate the population density for each state in each region and create dataframes
|
89 |
+
dataframes = []
|
90 |
for region, data in regions.items():
|
91 |
population = data['population']
|
92 |
area = data['area']
|
93 |
states = data['states']
|
94 |
+
densities = []
|
95 |
for state in states:
|
96 |
+
state_data = states_dict[state]
|
97 |
+
state_population = state_data['population']
|
98 |
+
state_area = state_data['area']
|
99 |
state_density = calculate_density(state_population, state_area)
|
100 |
+
densities.append(state_density)
|
101 |
+
df = pd.DataFrame({'State': states, 'Population Density': densities})
|
102 |
+
dataframes.append(df)
|
103 |
+
|
104 |
+
plot_graph(df, region)
|
105 |
+
|
106 |
+
# Use Streamlit to display dataframes
|
107 |
+
for df in dataframes:
|
108 |
+
st.write(df)
|