Aziz Alto
commited on
Commit
·
dcda531
1
Parent(s):
9517dd2
Upload example1.txt
Browse files- examples/example1.txt +54 -0
examples/example1.txt
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
World Population Dataset
|
| 3 |
+
|
| 4 |
+
Monitoring city population (per country) with upper/lower bounds intervals
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
# -- filter on arbitrarily selected countries
|
| 8 |
+
countries = df.sample(5)['country'].values
|
| 9 |
+
# countries = ["Japan", "Argentina", "Greece", "Thailand", "Peru", "Saudi Arabia", "Jordan" ,"United States"];
|
| 10 |
+
|
| 11 |
+
# -- add a new column to df (mean or std)
|
| 12 |
+
df = df.groupby("country", as_index=False).apply(
|
| 13 |
+
lambda d: d.assign(city_population_avg=d["population"].std().astype(int))
|
| 14 |
+
);
|
| 15 |
+
# -- measure members distance from LCL
|
| 16 |
+
df = df.groupby("country", as_index=False).apply(
|
| 17 |
+
lambda d: d.assign(upper_bound=abs((d["city_population_avg"]*1.1).astype(int)+d["city_population_avg"]))
|
| 18 |
+
);
|
| 19 |
+
df = df.groupby("country", as_index=False).apply(
|
| 20 |
+
lambda d: d.assign(lower_bound=abs((d["city_population_avg"]*1.1).astype(int)-d["city_population_avg"]))
|
| 21 |
+
);
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
for country in countries:
|
| 25 |
+
|
| 26 |
+
_df = df[df['country']==country]
|
| 27 |
+
b = px.bar(_df, x="city", y="population", facet_col="country", facet_col_wrap=4)
|
| 28 |
+
|
| 29 |
+
s = px.line(
|
| 30 |
+
_df, x="city", y="city_population_avg", facet_col="country", facet_col_wrap=4,
|
| 31 |
+
color="city_population_avg"
|
| 32 |
+
).update_traces(line_color="orange")
|
| 33 |
+
b.add_traces(s.data)
|
| 34 |
+
|
| 35 |
+
u = px.line(
|
| 36 |
+
_df, x="city", y="upper_bound", facet_col="country", facet_col_wrap=4,
|
| 37 |
+
color="upper_bound"
|
| 38 |
+
).update_traces(line_color="green")
|
| 39 |
+
b.add_traces(u.data)
|
| 40 |
+
|
| 41 |
+
l = px.line(
|
| 42 |
+
_df, x="city", y="lower_bound", facet_col="country", facet_col_wrap=4,
|
| 43 |
+
color="lower_bound"
|
| 44 |
+
).update_traces(line_color="red")
|
| 45 |
+
b.add_traces(l.data)
|
| 46 |
+
|
| 47 |
+
st.markdown(f"# {country}")
|
| 48 |
+
col1, col2 = st.columns(2)
|
| 49 |
+
col1.plotly_chart(b)
|
| 50 |
+
col2.write(_df)
|
| 51 |
+
|
| 52 |
+
below_control = _df[_df['population']<_df['lower_bound']].shape[0]
|
| 53 |
+
msg = f"{round((below_control/_df.shape[0]), 2)*100}% of the cities are below the `lower_bound` of population control"
|
| 54 |
+
st.markdown(f"> ### {msg}")
|