-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting_maps.py
334 lines (278 loc) · 9.19 KB
/
plotting_maps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
This is a script to plot maps of Uruguay using the data from the Encuesta Continua de Hogares (ECH)
from the Instituto Nacional de Estadística (INE) of Uruguay.
It is assumed that the data is in the folder 'data' and that the shapefile is in the folder 'data'.
It is not automated, so you have to change the code manually to plot the map you want.
"""
# %%
import glob
import os
import unicodedata
import fiona
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# %%
def detect_separator(filename):
with open(filename, "r") as file:
first_line = file.readline()
if first_line.count(";") > first_line.count(","):
return ";"
else:
return ","
# %%
def load_data_all_files(path) -> pd.DataFrame:
all_files = glob.glob(os.path.join(path, "*.csv"))
# all_files = all_files.sort()
li = []
for filename in all_files:
if filename != "todo.csv":
sep = detect_separator(filename)
df = pd.read_csv(filename, index_col=None, header=0, sep=sep)
df.columns = df.columns.str.lower()
df = drop_columns(df)
df.columns = df.columns.str.replace("_", "")
for column in df.columns:
if df[column].dtype == "object":
df[column] = df[column].str.lower()
if pd.api.types.is_string_dtype(df[column]):
df[column] = df[column].apply(remove_accents)
li.append(df)
return pd.concat(li, axis=0, ignore_index=True, join="outer")
# %%
def load_data(path) -> pd.DataFrame:
sep = detect_separator(path)
df = pd.read_csv(path, index_col=None, header=0, sep=sep)
return df
# %%
def drop_columns(df):
# List of columns to be dropped
cols_to_drop = [
"mes",
"secc",
"nper",
"subempleo",
]
# Drop columns that start with specific letters
cols_to_drop += [
col for col in df.columns if col.startswith(("f", "g", "h", "H", "W", "i"))
]
# Drop columns containing 'mto'
cols_to_drop += [col for col in df.columns if "mto" in col]
# Drop columns with 'PT' except for 'PT1'
cols_to_drop += [col for col in df.columns if "PT" in col and col != "PT1"]
# Drop columns with 'YT'
cols_to_drop += [col for col in df.columns if "YT" in col]
# Drop columns with '06'
cols_to_drop += [col for col in df.columns if "06" in col]
# Drop the columns from the dataframe
df = df.drop(columns=cols_to_drop)
return df
# %%
def handle_missing_values(df):
# Define a function to fill NaN with the mode, or a specified value if no mode
def fill_mode(col):
# Check if mode result is non-empty and the column is categorical
if not col.mode().empty and col.dtype == "object":
return col.fillna(col.mode().iloc[0])
else:
return col.fillna("Missing") # Filling NaNs with 'Missing' for categorical
# Apply the function to each column
df = df.apply(fill_mode, axis=0)
return df
def convert_to_int_else_object(value):
try:
# Attempt to convert to an integer
return int(value)
except (ValueError, TypeError):
# If conversion to an integer fails, return the value as a string
return value
# Define a function to encode categorical variables
def encode_categorical(df):
# Find categorical variables (assuming they are of type 'object')
le = LabelEncoder()
for col in df.select_dtypes(include=["object"]).columns:
df[col] = df[col].astype(str)
df[col] = le.fit_transform(df[col])
return df
# Function to remove accents from strings
def remove_accents(input_str):
if isinstance(input_str, str):
nfkd_form = unicodedata.normalize("NFKD", input_str)
only_ascii = nfkd_form.encode("ASCII", "ignore")
return only_ascii.decode("ASCII")
else:
return input_str
# %%
df = load_data_all_files("data/sexo")
# %%
# Handle missing values
df = handle_missing_values(df)
# %%
# Handle missing values
# %%
# Selected columns and label
y = df["pt1"]
df = df.drop(
columns=[
"pt2",
"pt4",
"ysvl",
"ytotsinagui",
"wsem",
"e582",
"egps8",
"egps5",
"e5611",
"e247",
"d2114",
"ysvlsag",
"e45311cv",
"e4521cv",
"pobre",
"pobpcoac",
"e5602",
"ysvlcons",
"d9",
"e2141",
"lecheenpol",
"d212",
"d2141",
"d16",
]
)
# %%
# Set the SHAPE_RESTORE_SHX option to YES
with fiona.Env(SHAPE_RESTORE_SHX="YES"):
uruguay_map = gpd.read_file("data/departamentos.geojson", encoding="utf-8")
# %%
uruguay_map
# %%
# set column values to lowercase
uruguay_map.columns = map(str.lower, uruguay_map.columns)
# rename column admlnm to nomdpto
uruguay_map.rename(columns={"admlnm": "nomdpto"}, inplace=True)
# %%
for column in uruguay_map.columns:
if uruguay_map[column].dtype == "object":
uruguay_map[column] = uruguay_map[column].str.lower()
uruguay_map
# %%
data = df.groupby("nomdpto")["pt1"].mean().reset_index()
data
# %%
merged_data = uruguay_map.merge(data, on="nomdpto", how="left")
merged_data
# dicide the column pt1 by 39,71
merged_data["pt1"] = merged_data["pt1"] / 39.71
# %%
# For a simple choropleth map
merged_data.plot(
column="pt1", scheme="naturalbreaks", figsize=(10, 10), legend=True, linewidth=4
)
# make the grid smaller
plt.grid(True, which="both", axis="both", linestyle="--")
plt.title("Average Salary by Department in Uruguay (U$S)")
plt.axis("off") # This will remove the axis
plt.show()
# %%
# %%
df["e49"] = df["e49"].replace({1: 1, 2: 0, 3: 1})
percentage_df = df.groupby("nomdpto")["e49"].mean().reset_index()
percentage_df["e49"] = percentage_df["e49"] * 100 # Convert to percentage
# %%
merged_data = uruguay_map.merge(percentage_df, on="nomdpto", how="left")
merged_data
# %%
# For a simple choropleth map
merged_data.plot(
column="e49", scheme="naturalbreaks", figsize=(10, 10), legend=True, linewidth=4
)
# make the grid smaller
plt.grid(True, which="both", axis="both", linestyle="--")
plt.title("Percentage of people that assited to school by Department in Uruguay")
plt.axis("off") # This will remove the axis
plt.show()
# %%
"Percentage of people that assited to school by Department in Uruguay".lower().replace(
" ", "_"
)
# %%
df["e511"] = df["e511"].replace("Missing", np.nan)
# Drop rows where 'e511' is NaN
df_ = df.dropna(subset=["e511"])
data = df_.groupby("nomdpto")["e511"].mean().reset_index()
data
# %%
merged_data = uruguay_map.merge(data, on="nomdpto", how="left")
merged_data
# %%
# For a simple choropleth map
merged_data.plot(
column="e511", scheme="naturalbreaks", figsize=(10, 10), legend=True, linewidth=4
)
# make the grid smaller
plt.grid(True, which="both", axis="both", linestyle="--")
plt.title(
"Average years of early childhood or preschool education by Department in Uruguay"
)
plt.axis("off") # This will remove the axis
plt.show()
# %%
"Average years of early childhood or preschool education by Department in Uruguay".lower().replace(
" ", "_"
)
# %%
df["e574"].unique()
# %%
# e574
df["e48"] = df["e48"].replace({1: 1, 2: 0})
# Calculate the percentage of people who can read and write per department
percentage_df = df.groupby("nomdpto")["e48"].mean().reset_index()
percentage_df["e48"] = percentage_df["e48"] * 100 # Convert to percentage
# Load your geographic data for the departments
# Replace 'path_to_geo_data' with the path to your shapefile or GeoJSON
# Merge the percentage data with the geographic data
merged_data = uruguay_map.merge(percentage_df, on="nomdpto", how="left")
# %%
# For a simple choropleth map
merged_data.plot(
column="e48", scheme="naturalbreaks", figsize=(10, 10), legend=True, linewidth=4
)
# make the grid smaller
plt.grid(True, which="both", axis="both", linestyle="--")
plt.title("Percentage of people who can read and write by Department in Uruguay")
plt.axis("off") # This will remove the axis
plt.show()
# %%
"Percentage of people who can read and write by Department in Uruguay".lower().replace(
" ", "_"
)
# %%
df["e2181"] = df["e2181"].replace({1: 1, 2: 0})
# Calculate the percentage of people who can read and write per department
percentage_df = df.groupby("nomdpto")["e2181"].mean().reset_index()
percentage_df["e2181"] = percentage_df["e2181"] * 100 # Convert to percentage
# Load your geographic data for the departments
# Replace 'path_to_geo_data' with the path to your shapefile or GeoJSON
# Merge the percentage data with the geographic data
merged_data = uruguay_map.merge(percentage_df, on="nomdpto", how="left")
# %%
# For a simple choropleth map
merged_data.plot(
column="e2181", scheme="naturalbreaks", figsize=(10, 10), legend=True, linewidth=4
)
# make the grid smaller
plt.grid(True, which="both", axis="both", linestyle="--")
plt.title(
"Percentage of people who completed tertiary university studies or similar by Department in Uruguay"
)
plt.axis("off") # This will remove the axis
plt.show()
# %%
"Percentage of people who completed tertiary university studies or similar by Department in Uruguay".lower().replace(
" ", "_"
)
# %%