-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtw_analysis_data.py
More file actions
5038 lines (4127 loc) · 214 KB
/
tw_analysis_data.py
File metadata and controls
5038 lines (4127 loc) · 214 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import gzip
import itertools
import json
import re
import time
import zipfile
import numpy as np
import pandas as pd
import io
import requests
import warnings
import sys
from datetime import datetime
from pyquery import PyQuery
from pandas.api.types import is_integer_dtype
from requests.adapters import HTTPAdapter
import urllib3
from urllib3.util import Retry
from pathlib import Path
from thefuzz import fuzz
from dateutil.relativedelta import relativedelta
from typing import Callable
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
warnings.simplefilter(action="ignore", category=FutureWarning)
# Regular expression for sanitizing strings for use as keys or filenames
FILENAME_SANITIZE_PATTERN = r'[- ,、()~∕\/-%*?:"<>|()—]+'
# Base directory for caching downloaded data
EXTRA_DATA_DIR = Path("./extraData/TW_Analysis")
# define the retry strategy
retry_strategy = Retry(
total=4, # maximum number of retries
backoff_factor=2,
status_forcelist=[
429,
500,
502,
503,
504,
], # the HTTP status codes to retry on
)
# create an HTTP adapter with the retry strategy and mount it to the session
adapter = HTTPAdapter(max_retries=retry_strategy)
# 5.4:requests.Session 非執行緒安全,改用 threading.local 讓每個執行緒擁有獨立 session
import threading
_thread_local = threading.local()
def _get_session() -> requests.Session:
"""取得目前執行緒的 Session,不存在則建立。"""
if not hasattr(_thread_local, "session"):
s = requests.Session()
if sys.platform.startswith("linux"):
s.headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Referer": "https://www.google.com/",
}
else:
s.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Referer": "https://www.google.com/",
}
s.mount("http://", adapter)
s.mount("https://", adapter)
_thread_local.session = s
return _thread_local.session
# 向後相容:保留 session 名稱,但存取時透過 _get_session()
# --- Helper Functions ---
def sanitize_filename(name: str) -> str:
"""Sanitizes a string to be used as a valid filename or key."""
return re.sub(FILENAME_SANITIZE_PATTERN, "_", name)
def _ensure_dir_exists(path: Path):
"""Ensures that the directory for the given path exists."""
path.parent.mkdir(parents=True, exist_ok=True)
def read_xml(url: str, xpath: str) -> pd.DataFrame:
r = _get_session().get(url, verify=False)
df = pd.read_xml(io.BytesIO(r.content), xpath=xpath)
return df
def read_xml_with_cache(path: Path, url: str, xpath: str) -> pd.DataFrame:
_ensure_dir_exists(path)
if not path.is_file():
r = _get_session().get(url, verify=False)
with gzip.open(path, "wb") as f:
f.write(r.content)
df = pd.read_xml(path, compression="gzip", xpath=xpath)
return df
def read_csv(url: str, encoding: str = "utf-8") -> pd.DataFrame:
r = _get_session().get(url, verify=False)
df = pd.read_csv(io.BytesIO(r.content), encoding=encoding)
return df
def read_csv_with_cache(path: Path, url: str, encoding: str = "utf-8") -> pd.DataFrame:
_ensure_dir_exists(path)
if not path.is_file():
r = _get_session().get(url, verify=False)
with gzip.open(path, "wb") as f:
f.write(r.content)
df = pd.read_csv(path, compression="gzip", encoding=encoding)
return df
def read_json(url: str, encoding: str = "utf-8") -> pd.DataFrame:
r = _get_session().get(url, verify=False)
df = pd.read_json(io.BytesIO(r.content), encoding=encoding)
return df
def read_excel_with_cache(
path: Path,
url: str,
read_func,
) -> pd.DataFrame:
_ensure_dir_exists(path)
if not path.is_file():
r = _get_session().get(url, verify=False)
with gzip.open(path, "wb") as f:
f.write(r.content)
with gzip.open(path, "rb") as f_gz:
excel_bytes = io.BytesIO(f_gz.read())
df = read_func(excel_bytes)
return df
# data =========================================================
# https://data.gov.tw/dataset/6019 消費者物價基本分類指數
def _make_df_csv_simple(
url: str, index_col: str, columns_remove_patt: str
) -> "Callable[[], pd.DataFrame]":
"""3.4:CSV 資料工廠函式。
針對「下載 CSV → set_index → 清理欄位名稱」的標準模式,
回傳一個符合相同簽章的 callable,供各 df_ 函式委派使用。
"""
def _loader():
df = read_csv(url)
df = df.set_index(index_col)
df.columns = df.columns.str.replace(columns_remove_patt, "", regex=True)
return df
return _loader
def df_消費者物價基本分類指數() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/230555/pr0101a1m.xml"
xpath = "//Obs"
item_remove_patt = r"\(指數基期:民國110年=100\)"
df = read_xml(url, xpath)
df["Item"] = df["Item"].str.replace(item_remove_patt, "", regex=True)
return df
# https://data.gov.tw/dataset/148439 生產者物價基本分類指數
def df_生產者物價基本分類指數() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/230534/pr0701a1m.xml"
xpath = "//Obs"
item_remove_patt = r"\(指數基期:民國110年=100\)"
df = read_xml(url, xpath)
df["Item"] = df["Item"].str.replace(item_remove_patt, "", regex=True)
return df
# https://data.gov.tw/dataset/8239 躉售物價基本分類指數
def df_躉售物價基本分類指數() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/230546/pr0202a1m.xml"
xpath = "//Obs"
item_remove_patt = r"\(民國105年=100\)"
df = read_xml(url, xpath)
df["Item"] = df["Item"].str.replace(item_remove_patt, "", regex=True)
return df
# https://data.gov.tw/dataset/6637 人力資源調查失業率
def df_人力資源調查失業率() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/230038/mp0101a07.xml"
xpath = "//失業率"
df = read_xml(url, xpath)
df = df.set_index("年月別_Year_and_month")
df.columns = df.columns.str.replace(r"_?[a-zA-Z_]*_百分比$", r"", regex=True).str.replace(
r"_[a-zA-Z_]+$", r"", regex=True
)
df = df.replace("-", np.nan)
df = df.astype(float) / 100
return df
# https://data.gov.tw/dataset/6640 人力資源調查縣市別失業率
def df_人力資源調查縣市別失業率() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/230038/mp0101a10.xml"
xpath = "//縣市別失業率"
df = read_xml(url, xpath)
df = df.set_index("年月別_Year_and_month")
df.columns = df.columns.str.replace(r"_[a-zA-Z_]+_百分比$", r"", regex=True)
df = df.replace("-", np.nan)
df = df.astype(float) / 100
return df
# https://data.gov.tw/dataset/31055 歷年人力資源調查重要指標
def df_歷年人力資源調查重要指標() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/234748/mp04014.xml"
xpath = "//歷年人力資源調查重要指標"
df = read_xml(url, xpath)
df = df.set_index("年月別_Year_and_month")
df = df.filter(regex=r"\d{4}$", axis="index")
df.index = df.index.str.replace(r".*(\d{4})$", r"\1", regex=True)
df.columns = df.columns.str.replace(r"_[0-9a-zA-Z_]+_百分比$", r"", regex=True).str.replace(
r"_[a-zA-Z_]+$", r"", regex=True
)
df = df.replace("-", np.nan)
df = df.astype(float) / 100
return df
# https://data.gov.tw/dataset/33442 人力資源調查重要指標
def df_人力資源調查重要指標() -> pd.DataFrame:
key = "人力資源調查重要指標"
key = sanitize_filename(key)
urls = {
104: "https://www.dgbas.gov.tw/public/data/open/Cen/Mp04037.xml",
105: "https://www.dgbas.gov.tw/public/data/open/Cen/Mp04037A105.xml",
106: "https://www.dgbas.gov.tw/public/data/open/Cen/Mp04037A106.xml",
107: "https://www.dgbas.gov.tw/public/data/open/Cen/Mp04037A107.xml",
108: "https://www.dgbas.gov.tw/public/data/open/Cen/Mp04037A108.xml",
109: "https://www.dgbas.gov.tw/public/data/open/Cen/Mp04037A109.xml",
110: "https://www.dgbas.gov.tw/public/data/open/Cen/Mp04037A110.xml",
111: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231127/mp04037a111.xml",
112: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/233304/mp04037a112.xml",
113: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/234748/mp04037a113.xml",
}
xpath = "//人力資源調查重要指標"
if max(urls.keys()) + 1911 + 1 < datetime.now().year and datetime.now().month > 4:
print(f"請更新 {key}")
df = []
for year, url in urls.items():
data = read_xml_with_cache(EXTRA_DATA_DIR / key / f"{year}.xml.gz", url, xpath)
data["年度"] = year + 1911 # 轉西元
data = data.rename(
columns={
"地區別_District_or_region": "按地區別分_District_or_region",
"年齡15歲以上民間人口占總人口之比率_Proportion_of_civilian_population_aged_15_years_and_over_to_total_population_百分比": "年齡15歲以上民間人口占總人口之比率_Proportion_of_civilian_population_aged_15_years_and_over_to_total_population",
"勞動力_占總人口之比率_Labor_force_Proportion_of_labor_force_to_total_population_百分比": "勞動力_占總人口之比率_Labor_force_Proportion_of_labor_force_to_total_population",
"勞動力_占15歲以上民間人口之比率_勞動力參與率_總計_Labor_force_Labor_force_participation_rate_Total_百分比": "勞動力_占15歲以上民間人口之比率_勞動力參與率_總計_Labor_force_Labor_force_participation_rate_Total",
"勞動力_占15歲以上民間人口之比率_勞動力參與率_男_Labor_force_Labor_force_participation_rate_Male_百分比": "勞動力_占15歲以上民間人口之比率_勞動力參與率_男_Labor_force_Labor_force_participation_rate_Male",
"勞動力_占15歲以上民間人口之比率_勞動力參與率_女_Labor_force_Labor_force_participation_rate_Female_百分比": "勞動力_占15歲以上民間人口之比率_勞動力參與率_女_Labor_force_Labor_force_participation_rate_Female",
"就業者_占總人口之比率_Employed_Proportion_of_employed_persons_to_total_population_百分比": "就業者_占總人口之比率_Employed_Proportion_of_employed_persons_to_total_population",
"就業者_占15歲以上民間人口之比率_Employed_Proportion_of_employed_persons_to_civilian_population_aged_15_years_and_over_百分比": "就業者_占15歲以上民間人口之比率_Employed_Proportion_of_employed_persons_to_civilian_population_aged_15_years_and_over",
"就業者_占勞動力之比率_Employed_Proportion_of_employed_persons_to_labor_force_百分比": "就業者_占勞動力之比率_Employed_Proportion_of_employed_persons_to_labor_force",
"失業率_總計_Unemployment_rate_Total_百分比": "失業率_總計_Unemployment_rate_Total",
"失業率_男_Unemployment_rate_Male_百分比": "失業率_男_Unemployment_rate_Male",
"失業率_女_Unemployment_rate_Female_百分比": "失業率_女_Unemployment_rate_Female",
"年齡15歲以上民間人口占總人口之比率_Proportion_of_civilian_population_age_15_and_above_to_total_population_百分比": "年齡15歲以上民間人口占總人口之比率_Proportion_of_civilian_population_aged_15_years_and_over_to_total_population",
"就業者_占15歲以上民間人口之比率_Employed_Proportion_of_employed_persons_to_civilian_population_age_15_and_above_百分比": "就業者_占15歲以上民間人口之比率_Employed_Proportion_of_employed_persons_to_civilian_population_aged_15_years_and_over",
}
)
df.append(data)
df = pd.concat(df, ignore_index=True)
df["按地區別分_District_or_region"] = (
df["按地區別分_District_or_region"].str.strip().str.replace(r"[a-zA-Z ]+", "", regex=True)
)
df.columns = df.columns.str.replace(r"[_a-zA-Z0-9]+$", "", regex=True)
num_columns = [
column for column in df.columns if "年度" not in column and "地區別分" not in column
]
df[num_columns] = df.loc[:, num_columns].replace("-", np.nan).astype(float) / 100
return df
# https://data.gov.tw/dataset/32741 歷年教育程度別失業率
def df_歷年教育程度別失業率() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/234748/mp04033.xml"
xpath = "//歷年教育程度別失業率"
df = read_xml(url, xpath)
df = df.set_index("年月別_Year_and_month")
df = df.filter(regex=r"\d{4}$", axis="index")
df.index = df.index.str.replace(r".*(\d{4})$", r"\1", regex=True)
df.columns = df.columns.str.replace(r"_[_a-zA-Z]+_百分比", "", regex=True)
df = df.replace("-", np.nan)
df = df.astype(float) / 100
return df
# https://data.gov.tw/dataset/34118 教育程度別失業率
def df_教育程度別失業率() -> pd.DataFrame:
key = "教育程度別失業率"
key = sanitize_filename(key)
urls = {
104: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04049.xml",
105: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04049A105.xml",
106: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04049A106.xml",
107: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04049A107.xml",
108: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04049A108.xml",
109: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04049A109.xml",
110: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04049A110.xml",
111: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/mp04049a111.xml",
112: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/233304/mp04049a112.xml",
113: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/234748/mp04049a113.xml",
}
xpath = "//教育程度別失業率"
if max(urls.keys()) + 1911 + 1 < datetime.now().year and datetime.now().month > 4:
print(f"請更新 {key}")
df = []
for year, url in urls.items():
data = read_xml_with_cache(EXTRA_DATA_DIR / key / f"{year}.xml.gz", url, xpath)
data["年度"] = year + 1911 # 轉西元
data = data.rename(
columns={
"地區別_District_or_region": "按地區別分_District_or_region",
"高級中等_高中_高職_女_Senior_high_school_regular_and_vocational_Senior_high_and_vocational_Female_百分比": "高級中等_高中_高職_女_Senior_high_school_regular_and_vocational_Female_百分比",
}
)
df.append(data)
df = pd.concat(df, ignore_index=True)
df["按地區別分_District_or_region"] = (
df["按地區別分_District_or_region"].str.strip().str.replace(r"[a-zA-Z ]+", "", regex=True)
)
df.columns = df.columns.str.replace(r"_[_a-zA-Z]+_百分比", "", regex=True)
num_columns = [
column for column in df.columns if "年度" not in column and "地區別分" not in column
]
df[num_columns] = df.loc[:, num_columns].replace("-", np.nan).astype(float) / 100
return df
# https://data.gov.tw/dataset/32743 歷年年齡組別失業率
def df_歷年年齡組別失業率() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/234748/mp04031.xml"
xpath = "//歷年年齡組別失業率"
df = read_xml(url, xpath)
df = df.set_index("年月別_Year_and_month")
df = df.filter(regex=r"\d{4}$", axis="index")
df.index = df.index.str.replace(r".*(\d{4})$", r"\1", regex=True)
df.columns = df.columns.str.replace(
r"_(合計|小計|男|女).*_百分比$", r"_\1", regex=True
).str.replace(r"_[a-zA-Z]+_百分比$", "", regex=True)
df = df.replace("-", np.nan)
df = df.astype(float) / 100
return df
# https://data.gov.tw/dataset/34117 年齡組別失業率
def df_年齡組別失業率() -> pd.DataFrame:
key = "年齡組別失業率"
key = sanitize_filename(key)
urls = {
104: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04048.xml",
105: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04048A105.xml",
106: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04048A106.xml",
107: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04048A107.xml",
108: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04048A108.xml",
109: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04048A109.xml",
110: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/Mp04048A110.xml",
111: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231813/mp04048a111.xml",
112: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/233304/mp04048a112.xml",
113: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/234748/mp04048a113.xml",
}
xpath = "//年齡組別失業率"
if max(urls.keys()) + 1911 + 1 < datetime.now().year and datetime.now().month > 4:
print(f"請更新 {key}")
df = []
for year, url in urls.items():
data = read_xml_with_cache(EXTRA_DATA_DIR / key / f"{year}.xml.gz", url, xpath)
data["年度"] = year + 1911 # 轉西元
data = data.rename(
columns={
"地區別_District_or_region": "按地區別分_District_or_region",
"年齡65歲以上_男_65_years_and_above_Male_百分比": "年齡65歲以上_男_65_years_and_over_Male_百分比",
"年齡65歲以上_女_65_years_and_above_Female_百分比": "年齡65歲以上_女_65_years_and_over_Female_百分比",
"年齡65歲以上_合計_65_years_and_above_Total_百分比": "年齡65歲以上_合計_65_years_and_over_Total_百分比",
}
)
df.append(data)
df = pd.concat(df, ignore_index=True)
df["按地區別分_District_or_region"] = (
df["按地區別分_District_or_region"].str.strip().str.replace(r"[a-zA-Z ]+", "", regex=True)
)
df.columns = df.columns.str.replace(
r"_(合計|小計|男|女).*_百分比$", r"_\1", regex=True
).str.replace(r"_[a-zA-Z]+_百分比$", "", regex=True)
num_columns = [
column for column in df.columns if "年度" not in column and "地區別分" not in column
]
df[num_columns] = df.loc[:, num_columns].replace("-", np.nan).astype(float) / 100
return df
# https://data.gov.tw/dataset/37971 教育程度別失業率—按年齡分
def df_教育程度別失業率_按年齡分() -> pd.DataFrame:
key = "教育程度別失業率—按年齡分"
key = sanitize_filename(key)
urls = {
104: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231804/Mp04068.xml",
105: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231804/Mp04068A105.xml",
106: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231804/Mp04068A106.xml",
107: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231804/Mp04068A107.xml",
108: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231804/Mp04068A108.xml",
109: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231804/Mp04068A109.xml",
110: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231804/Mp04068A110.xml",
111: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/231804/mp04068a111.xml",
112: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/233304/mp04068a112.xml",
113: "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/234748/mp04068a113.xml",
}
xpath = "//教育程度別失業率_按年齡分"
if max(urls.keys()) + 1911 + 1 < datetime.now().year and datetime.now().month > 4:
print(f"請更新 {key}")
df = []
for year, url in urls.items():
data = read_xml_with_cache(EXTRA_DATA_DIR / key / f"{year}.xml.gz", url, xpath)
data["年度"] = year + 1911 # 轉西元
data = data[~data["項目別_Item"].str.contains("按")]
df.append(data)
df = pd.concat(df, ignore_index=True)
df["項目別_Item"] = (
df["項目別_Item"]
.str.strip()
.str.replace(r"[a-zA-Z ]+", "", regex=True)
.str.replace("~", "~")
.str.replace("0", "0")
.str.replace("1", "1")
.str.replace("2", "2")
.str.replace("3", "3")
.str.replace("4", "4")
.str.replace("5", "5")
.str.replace("6", "6")
.str.replace("9", "9")
)
df.columns = df.columns.str.replace(r"_[a-zA-Z_]+_百分比$", "", regex=True)
num_columns = [
column for column in df.columns if "年度" not in column and "項目別" not in column
]
df[num_columns] = df.loc[:, num_columns].replace("-", np.nan).astype(float) / 100
return df
# https://data.gov.tw/dataset/151323 就業率
def df_就業率() -> pd.DataFrame:
url = "https://www.gender.ey.gov.tw/GecDB/Common/OpenXML.ashx?sn=$mQvpHYEayTTt8pmhMjRvA@@"
xpath = "//DataTable"
df_教育程度別 = read_xml(url, xpath)
df_教育程度別["Period"] /= 100
df_教育程度別["Period"] = df_教育程度別["Period"].astype(int)
df_教育程度別 = df_教育程度別.pivot_table(
values="Val", columns=["Category1Title", "Category2Title"], index="Period"
)
df_教育程度別.columns = [f"{kind}_{edu}" for kind, edu in df_教育程度別.columns]
df_教育程度別 = df_教育程度別.replace("-", np.nan)
df_教育程度別 = df_教育程度別.astype(float) / 100
df_教育程度別.index = df_教育程度別.index + 1911 # 轉西元
url = "https://www.gender.ey.gov.tw/GecDB/Common/OpenXML.ashx?sn=oa8xEQOEl3KZNyQ8EOJT3A@@"
xpath = "//DataTable"
df_年齡別 = read_xml(url, xpath)
df_年齡別["Period"] /= 100
df_年齡別["Period"] = df_年齡別["Period"].astype(int)
df_年齡別 = df_年齡別.pivot_table(
values="Val", columns=["Category1Title", "Category2Title"], index="Period"
)
df_年齡別.columns = [f"{kind}_{edu}" for kind, edu in df_年齡別.columns]
df_年齡別 = df_年齡別.replace("-", np.nan)
df_年齡別 = df_年齡別.astype(float) / 100
df_年齡別.index = df_年齡別.index + 1911 # 轉西元
return df_教育程度別, df_年齡別
# https://data.gov.tw/dataset/39495 主要國家零歲平均餘命
def df_主要國家零歲平均餘命() -> pd.DataFrame:
url = "https://ws.ndc.gov.tw/001/administrator/10/relfile/0/13729/ce34adb0-b1c0-4b55-a271-9b95b817811e.csv"
df = read_csv(url)
df = df.rename(columns={"男性零歲平均餘命(歲)": "男", "女性零歲平均餘命(歲)": "女"})
df[["男", "女"]] = df[["男", "女"]].replace("-", np.nan).astype(float)
df = df.pivot_table(values=["男", "女"], columns="國別", index="西元年")
df = df.swaplevel(axis="columns")
df = df.sort_index(axis="columns")
return df
# https://pip.moi.gov.tw/Publicize/Info/E2010 房價所得比
def df_房價所得比() -> pd.DataFrame:
key = "房價所得比"
key = sanitize_filename(key)
url = "https://pip.moi.gov.tw/Publicize/Info/E2010Data"
path = EXTRA_DATA_DIR / f"{key}.csv.gz"
_ensure_dir_exists(path)
try:
r = _get_session().get(
url,
params={
"dataGroup": "group03",
"f01": "091Q1",
"f02": f"{datetime.today().year-1911}Q4",
"f03": "TAIWAN|全國,F|新北市,A|台北市,H|桃園市,B|台中市,D|台南市,E|高雄市,G|宜蘭縣,J|新竹縣,K|苗栗縣,N|彰化縣,M|南投縣,P|雲林縣,Q|嘉義縣,T|屏東縣,V|台東縣,U|花蓮縣,X|澎湖縣,C|基隆市,O|新竹市,I|嘉義市,",
},
)
json_data = json.loads(r.content)
columns = json_data["resultTable"][0]
narrays = np.array(json_data["resultTable"][1:])
df = pd.DataFrame(narrays, columns=columns)
split_年 = df["年度季別"].str.split("Q", expand=True)
df["年度季別"] = (split_年[0].astype(int) + 1911).astype(str) + "Q" + split_年[1] # 轉西元
df = df.set_index("年度季別")
df = df.sort_index()
df = df.astype(float)
df.to_csv(path, compression="gzip")
except Exception as e:
print(f"❌ 更新失敗,讀取舊資料")
print(f"錯誤類型:{type(e).__name__}")
print(f"錯誤訊息:{e}")
df = pd.read_csv(path, index_col="年度季別", compression="gzip")
return df
# https://pip.moi.gov.tw/Publicize/Info/E2010 貸款負擔率
def df_貸款負擔率() -> pd.DataFrame:
key = "貸款負擔率"
key = sanitize_filename(key)
url = "https://pip.moi.gov.tw/Publicize/Info/E2010Data"
path = EXTRA_DATA_DIR / f"{key}.csv.gz"
_ensure_dir_exists(path)
if not path.is_file():
r = _get_session().get(
url,
params={
"dataGroup": "group04",
"f01": "091Q1",
"f02": f"{datetime.today().year-1911}Q4",
"f03": "TAIWAN|全國,F|新北市,A|台北市,H|桃園市,B|台中市,D|台南市,E|高雄市,G|宜蘭縣,J|新竹縣,K|苗栗縣,N|彰化縣,M|南投縣,P|雲林縣,Q|嘉義縣,T|屏東縣,V|台東縣,U|花蓮縣,X|澎湖縣,C|基隆市,O|新竹市,I|嘉義市,",
},
)
json_data = json.loads(r.content)
columns = json_data["resultTable"][0]
narrays = np.array(json_data["resultTable"][1:])
df = pd.DataFrame(narrays, columns=columns)
split_年 = df["年度季別"].str.split("Q", expand=True)
df["年度季別"] = (split_年[0].astype(int) + 1911).astype(str) + "Q" + split_年[1] # 轉西元
df = df.set_index("年度季別")
df = df.sort_index()
df = df.astype(float) / 100
df.to_csv(path, compression="gzip")
else:
df = pd.read_csv(path, index_col="年度季別", compression="gzip")
return df
# https://data.gov.tw/dataset/44232 國民所得統計-國民所得、儲蓄與投資-季
def df_國民所得統計_國民所得_儲蓄與投資_季() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/230514/na8201a1q.xml"
xpath = "//Obs"
df = read_xml(url, xpath)
df = df.fillna(0)
index_million = df["Item"].str.contains("百萬元") & (df["TYPE"] == "原始值")
df.loc[index_million, "Item_VALUE"] *= 1000000
df["Item"] = df["Item"].str.replace("百萬元", "元")
return df
# https://data.gov.tw/dataset/6799 國民所得統計-常用資料-季
def df_國民所得統計_常用資料_季() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/230514/na8101a1q.xml"
xpath = "//Obs"
df = read_xml(url, xpath)
index_million = df["Item"].str.contains("百萬") & (df["TYPE"] == "原始值")
df.loc[index_million, "Item_VALUE"] *= 1000000
df["Item"] = df["Item"].str.replace("百萬元", "元")
df["Item"] = df["Item"].str.replace("百萬美元", "美元")
return df
# https://data.gov.tw/dataset/6429 家庭收支調查-家庭收支重要指標
def df_家庭收支調查_家庭收支重要指標() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/001-%E5%AE%B6%E5%BA%AD%E6%94%B6%E6%94%AF%E9%87%8D%E8%A6%81%E6%8C%87%E6%A8%99.csv"
index_col = "年"
df = read_csv(url)
df = df.dropna()
df[index_col] = df[index_col].astype(int)
df = df.set_index(index_col)
return df
# https://data.gov.tw/dataset/9421 家庭收支調查-所得總額按來源別分
def df_家庭收支調查_所得總額按來源別分() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/013-%E6%89%80%E5%BE%97%E7%B8%BD%E9%A1%8D%E6%8C%89%E4%BE%86%E6%BA%90%E5%88%A5%E5%88%86.csv"
columns_remove_patt = "-億元"
index_col = "年"
df = read_csv(url)
df = df.dropna()
df[index_col] = df[index_col].astype(int)
億元_columns = [col for col in df.columns if "億元" in col]
df[億元_columns] = df[億元_columns] * 100000000
df = df.set_index(index_col)
df.columns = df.columns.str.replace(columns_remove_patt, "", regex=True)
return df
# https://data.gov.tw/dataset/24137 家庭收支調查-所得總額與可支配所得
def df_家庭收支調查_所得總額與可支配所得() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/034-%E6%89%80%E5%BE%97%E7%B8%BD%E9%A1%8D%E8%88%87%E5%8F%AF%E6%94%AF%E9%85%8D%E6%89%80%E5%BE%97.csv"
index_col = "年"
df = read_csv(url)
億元_columns = [col for col in df.columns if "億元" in col]
df[億元_columns] = df[億元_columns] * 100000000
df = df.set_index(index_col)
df.columns = df.columns.str.replace("-億元", "", regex=True)
df.columns = df.columns.str.replace("-元", "", regex=True)
return df
# https://data.gov.tw/dataset/22230 家庭收支調查-所得收入者人數與按年齡組別及性別之分配
def df_家庭收支調查_所得收入者人數與按年齡組別及性別之分配() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/029-%E6%89%80%E5%BE%97%E6%94%B6%E5%85%A5%E8%80%85%E4%BA%BA%E6%95%B8%E8%88%87%E6%8C%89%E5%B9%B4%E9%BD%A1%E7%B5%84%E5%88%A5%E5%8F%8A%E6%80%A7%E5%88%A5%E4%B9%8B%E5%88%86%E9%85%8D.csv"
index_col = "年"
df = read_csv(url)
df = df.dropna()
df[index_col] = df[index_col].astype(int)
df = df.set_index(index_col)
df.columns = df.columns.str.replace("-百分比", "", regex=True)
df[df.columns[1:]] = df[df.columns[1:]] / 100
return df
# https://data.gov.tw/dataset/24140 家庭收支調查-戶內人數別平均每戶所得總額
def df_家庭收支調查_戶內人數別平均每戶所得總額() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/037-平均每戶所得總額按戶內人數別分.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24141 家庭收支調查-戶內人數別平均每戶所得收入總計
def df_家庭收支調查_戶內人數別平均每戶所得收入總計() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/038-平均每戶所得收入總計按戶內人數別分.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24734 家庭收支調查-戶內人數別平均每戶非消費支出
def df_家庭收支調查_戶內人數別平均每戶非消費支出() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/041-平均每戶非消費支出按戶內人數別分.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24138 家庭收支調查-戶內人數別平均每戶可支配所得
def df_家庭收支調查_戶內人數別平均每戶可支配所得() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/035-平均每戶可支配所得按戶內人數別分.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24733 家庭收支調查-戶內人數別平均每戶消費支出
def df_家庭收支調查_戶內人數別平均每戶消費支出() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/040-平均每戶消費支出按戶內人數別分.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24735 家庭收支調查-戶內人數別平均每戶經常性支出
def df_家庭收支調查_戶內人數別平均每戶經常性支出() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/042-平均每戶經常性支出按戶內人數別分.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24139 家庭收支調查-戶內人數別平均每戶儲蓄
def df_家庭收支調查_戶內人數別平均每戶儲蓄() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/036-平均每戶儲蓄按戶內人數別分.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24731 家庭收支調查-戶內人數別平均每戶受僱人員報酬
def df_家庭收支調查_戶內人數別平均每戶受僱人員報酬() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/039-平均每戶受僱人員報酬按戶內人數別分.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/9559 家庭收支調查-家庭戶數按戶內人口規模別之分配
def df_家庭收支調查_家庭戶數按戶內人口規模別之分配() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/017-%E5%AE%B6%E5%BA%AD%E6%88%B6%E6%95%B8%E6%8C%89%E6%88%B6%E5%85%A7%E4%BA%BA%E5%8F%A3%E8%A6%8F%E6%A8%A1%E5%88%A5%E4%B9%8B%E5%88%86%E9%85%8D.csv",
"年",
r"\(%\)",
)()
# https://data.gov.tw/dataset/22236 家庭收支調查-農家平均每戶所得總額按來自農業與非農業分
def df_家庭收支調查_農家平均每戶所得總額按來自農業與非農業分() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/032-%E8%BE%B2%E5%AE%B6%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E6%89%80%E5%BE%97%E7%B8%BD%E9%A1%8D%E6%8C%89%E4%BE%86%E8%87%AA%E8%BE%B2%E6%A5%AD%E8%88%87%E9%9D%9E%E8%BE%B2%E6%A5%AD%E5%88%86.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24736 家庭收支調查-農家與非農家平均每戶可支配所得、所得總額及儲蓄
def df_家庭收支調查_農家與非農家平均每戶可支配所得_所得總額及儲蓄() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/043-%E8%BE%B2%E5%AE%B6%E8%88%87%E9%9D%9E%E8%BE%B2%E5%AE%B6%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E5%8F%AF%E6%94%AF%E9%85%8D%E6%89%80%E5%BE%97%E3%80%81%E6%89%80%E5%BE%97%E7%B8%BD%E9%A1%8D%E5%8F%8A%E5%84%B2%E8%93%84.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/27962 家庭收支調查-農家與非農家平均每戶消費支出、非消費支出及經常性支出
def df_家庭收支調查_農家與非農家平均每戶消費支出_非消費支出及經常性支出() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/045-%E8%BE%B2%E5%AE%B6%E8%88%87%E9%9D%9E%E8%BE%B2%E5%AE%B6%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E6%B6%88%E8%B2%BB%E6%94%AF%E5%87%BA%E3%80%81%E9%9D%9E%E6%B6%88%E8%B2%BB%E6%94%AF%E5%87%BA%E5%8F%8A%E7%B6%93%E5%B8%B8%E6%80%A7%E6%94%AF%E5%87%BA.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/24737 家庭收支調查-農家與非農家平均每戶所得收入總計及受僱人員報酬
def df_家庭收支調查_農家與非農家平均每戶所得收入總計及受僱人員報酬() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/044-%E8%BE%B2%E5%AE%B6%E8%88%87%E9%9D%9E%E8%BE%B2%E5%AE%B6%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E6%89%80%E5%BE%97%E6%94%B6%E5%85%A5%E7%B8%BD%E8%A8%88%E5%8F%8A%E5%8F%97%E5%83%B1%E4%BA%BA%E5%93%A1%E5%A0%B1%E9%85%AC.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/93951 家庭收支調查-戶數五等分位之平均每戶所得總額
def df_家庭收支調查_戶數五等分位之平均每戶所得總額() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/067-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E6%89%80%E5%BE%97%E7%B8%BD%E9%A1%8D.csv",
"年",
r"可支配所得按戶數五等分位組之平均每戶所得總額-|-元",
)()
# https://data.gov.tw/dataset/94752 家庭收支調查-戶數五等分位之平均每戶所得收入總計
def df_家庭收支調查_戶數五等分位之平均每戶所得收入總計() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/068-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E6%89%80%E5%BE%97%E6%94%B6%E5%85%A5%E7%B8%BD%E8%A8%88.csv",
"年",
r"可支配所得按戶數五等分位組之平均每戶所得收入總計-|-元",
)()
# https://data.gov.tw/dataset/98835 家庭收支調查-戶數五等分位之平均每戶非消費支出
def df_家庭收支調查_戶數五等分位之平均每戶非消費支出() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/071-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E9%9D%9E%E6%B6%88%E8%B2%BB%E6%94%AF%E5%87%BA.csv",
"年",
r"可支配所得按戶數五等分位組之平均每戶非消費支出-|-元",
)()
# https://data.gov.tw/dataset/91925 家庭收支調查-戶數五等分位之平均每戶可支配所得
def df_家庭收支調查_戶數五等分位之平均每戶可支配所得() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/065-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E5%8F%AF%E6%94%AF%E9%85%8D%E6%89%80%E5%BE%97.csv",
"年",
r"可支配所得按戶數五等分位組-|-元",
)()
# https://data.gov.tw/dataset/98834 家庭收支調查-戶數五等分位之平均每戶消費支出
def df_家庭收支調查_戶數五等分位之平均每戶消費支出() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/070-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E6%B6%88%E8%B2%BB%E6%94%AF%E5%87%BA.csv",
"年",
r"可支配所得按戶數五等分位組之平均每戶消費支出-|-元",
)()
# https://data.gov.tw/dataset/101338 家庭收支調查-戶數五等分位之平均每戶經常性支出
def df_家庭收支調查_戶數五等分位之平均每戶經常性支出() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/072-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E7%B6%93%E5%B8%B8%E6%80%A7%E6%94%AF%E5%87%BA.csv",
"年",
r"可支配所得按戶數五等分位組之平均每戶經常性支出-|-元",
)()
# https://data.gov.tw/dataset/93950 家庭收支調查-戶數五等分位之平均每戶儲蓄
def df_家庭收支調查_戶數五等分位之平均每戶儲蓄() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/066-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E5%84%B2%E8%93%84.csv",
"年",
r"可支配所得按戶數五等分位組之平均每戶儲蓄-|-元",
)()
# https://data.gov.tw/dataset/94754 家庭收支調查-戶數五等分位之平均每戶受僱人員報酬
def df_家庭收支調查_戶數五等分位之平均每戶受僱人員報酬() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/069-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E5%8F%97%E5%83%B1%E4%BA%BA%E5%93%A1%E5%A0%B1%E9%85%AC.csv",
"年",
r"可支配所得按戶數五等分位組之平均每戶受僱人員報酬-|-元",
)()
# https://data.gov.tw/dataset/9424 家庭收支調查-戶數五等分位組之平均每戶人口數
def df_家庭收支調查_戶數五等分位組之平均每戶人口數() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/019-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E4%BA%BA%E5%8F%A3%E6%95%B8.csv"
columns_remove_patt = "-人"
index_col = "年"
df = read_csv(url)
df = df.dropna()
df[index_col] = df[index_col].astype(int)
df = df.set_index(index_col)
df.columns = df.columns.str.replace(columns_remove_patt, "", regex=True)
return df
# https://data.gov.tw/dataset/9425 家庭收支調查-戶數五等分位組之平均每戶就業人數
def df_家庭收支調查_戶數五等分位組之平均每戶就業人數() -> pd.DataFrame:
url = "https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/020-%E6%88%B6%E6%95%B8%E4%BA%94%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E4%B9%8B%E5%B9%B3%E5%9D%87%E6%AF%8F%E6%88%B6%E5%B0%B1%E6%A5%AD%E4%BA%BA%E6%95%B8.csv"
columns_remove_patt = "-人"
index_col = "年"
df = read_csv(url)
df = df.dropna()
df[index_col] = df[index_col].astype(int)
df = df.set_index(index_col)
df.columns = df.columns.str.replace(columns_remove_patt, "", regex=True)
return df
# https://data.gov.tw/dataset/9422 家庭收支調查-戶數十等分位組分界點之可支配所得
def df_家庭收支調查_戶數十等分位組分界點之可支配所得() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/014-%E6%88%B6%E6%95%B8%E5%8D%81%E7%AD%89%E5%88%86%E4%BD%8D%E7%B5%84%E5%88%86%E7%95%8C%E9%BB%9E%E4%B9%8B%E5%8F%AF%E6%94%AF%E9%85%8D%E6%89%80%E5%BE%97.csv",
"年",
r"-元",
)()
# https://data.gov.tw/dataset/132285 家庭收支調查-所得收入者五等分位平均每人可支配所得
def df_家庭收支調查_所得收入者五等分位平均每人可支配所得() -> pd.DataFrame:
"""3.4:委派至 _make_df_csv_simple 工廠函式。"""
return _make_df_csv_simple(
"https://ws.dgbas.gov.tw/001/Upload/461/relfile/11525/232214/089-所得收入者平均每人可支配所得按五等分位組分.csv",