-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processor.py
More file actions
338 lines (312 loc) · 9.08 KB
/
data_processor.py
File metadata and controls
338 lines (312 loc) · 9.08 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
"""
資料處理工具模組
用於處理各種格式的地理資料
"""
import pandas as pd
import json
import requests
from typing import Dict, List, Optional
class WorldDataProcessor:
"""世界資料處理器"""
def __init__(self):
self.country_codes = self._load_country_codes()
def _load_country_codes(self) -> Dict[str, str]:
"""載入國家代碼對照表"""
# 簡化的國家代碼對照
codes = {
"China": "CHN",
"India": "IND",
"United States": "USA",
"Indonesia": "IDN",
"Pakistan": "PAK",
"Brazil": "BRA",
"Nigeria": "NGA",
"Bangladesh": "BGD",
"Russia": "RUS",
"Mexico": "MEX",
"Japan": "JPN",
"Germany": "DEU",
"Iran": "IRN",
"Turkey": "TUR",
"Vietnam": "VNM",
"Philippines": "PHL",
"Ethiopia": "ETH",
"Egypt": "EGY",
"United Kingdom": "GBR",
"France": "FRA",
"Italy": "ITA",
"South Africa": "ZAF",
"Tanzania": "TZA",
"Kenya": "KEN",
"Uganda": "UGA",
"Algeria": "DZA",
"Sudan": "SDN",
"Ukraine": "UKR",
"Iraq": "IRQ",
"Afghanistan": "AFG",
"Poland": "POL",
"Canada": "CAN",
"Morocco": "MAR",
"Saudi Arabia": "SAU",
"Uzbekistan": "UZB",
"Peru": "PER",
"Angola": "AGO",
"Malaysia": "MYS",
"Mozambique": "MOZ",
"Ghana": "GHA",
"Yemen": "YEM",
"Nepal": "NPL",
"Venezuela": "VEN",
"Madagascar": "MDG",
"Cameroon": "CMR",
}
return codes
def load_csv_data(self, file_path: str) -> pd.DataFrame:
"""載入 CSV 資料"""
try:
df = pd.read_csv(file_path, encoding="utf-8")
return df
except UnicodeDecodeError:
# 嘗試其他編碼
for encoding in ["gbk", "big5", "latin-1"]:
try:
df = pd.read_csv(file_path, encoding=encoding)
return df
except:
continue
raise ValueError(f"無法讀取檔案 {file_path}")
def add_country_codes(
self, df: pd.DataFrame, country_column: str = "country"
) -> pd.DataFrame:
"""為資料框加上國家ISO代碼"""
df = df.copy()
df["iso_alpha"] = df[country_column].map(self.country_codes)
# 找出沒有對應代碼的國家
missing_codes = df[df["iso_alpha"].isna()][country_column].unique()
if len(missing_codes) > 0:
print(f"⚠️ 以下國家沒有ISO代碼對照:{list(missing_codes)}")
return df
def validate_data(self, df: pd.DataFrame) -> Dict[str, any]:
"""驗證資料品質"""
validation_report = {
"total_rows": len(df),
"missing_data": {},
"data_types": {},
"summary_stats": {},
}
# 檢查缺失值
for col in df.columns:
missing_count = df[col].isna().sum()
missing_pct = (missing_count / len(df)) * 100
validation_report["missing_data"][col] = {
"count": missing_count,
"percentage": round(missing_pct, 2),
}
# 檢查資料類型
validation_report["data_types"] = df.dtypes.to_dict()
# 數值欄位的統計摘要
numeric_cols = df.select_dtypes(include=["int64", "float64"]).columns
for col in numeric_cols:
validation_report["summary_stats"][col] = {
"min": df[col].min(),
"max": df[col].max(),
"mean": df[col].mean(),
"median": df[col].median(),
}
return validation_report
def create_sample_world_data(self) -> pd.DataFrame:
"""建立範例世界資料集"""
data = {
"country": [
"China",
"India",
"United States",
"Indonesia",
"Pakistan",
"Brazil",
"Nigeria",
"Bangladesh",
"Russia",
"Mexico",
"Japan",
"Germany",
"Iran",
"Turkey",
"Vietnam",
"Philippines",
"Ethiopia",
"Egypt",
"United Kingdom",
"France",
"Italy",
"South Africa",
"Tanzania",
"Kenya",
"Uganda",
],
"population_2023": [
1439323776,
1380004385,
331002651,
273523615,
220892340,
212559417,
206139589,
164689383,
145934462,
128932753,
126476461,
83783942,
83992949,
84339067,
97338579,
109581078,
114963588,
102334404,
67886011,
65273511,
60461826,
59308690,
59734218,
53771296,
45741007,
],
"gdp_2023": [
17734.1,
3735.9,
25462.7,
1417.4,
374.7,
2055.5,
440.8,
460.2,
2240.4,
1688.9,
4937.4,
4259.9,
231.3,
815.3,
408.8,
394.1,
111.3,
469.4,
3131.4,
2937.5,
2107.7,
419.0,
67.8,
109.1,
47.7,
],
"life_expectancy": [
76.9,
69.4,
78.9,
71.7,
67.3,
75.9,
54.7,
72.6,
72.6,
75.1,
84.6,
81.3,
76.7,
77.7,
75.4,
71.2,
66.6,
72.0,
81.3,
82.7,
83.4,
64.1,
65.5,
66.7,
63.4,
],
"continent": [
"Asia",
"Asia",
"North America",
"Asia",
"Asia",
"South America",
"Africa",
"Asia",
"Europe",
"North America",
"Asia",
"Europe",
"Asia",
"Asia",
"Asia",
"Asia",
"Africa",
"Africa",
"Europe",
"Europe",
"Europe",
"Africa",
"Africa",
"Africa",
"Africa",
],
"capital": [
"Beijing",
"New Delhi",
"Washington D.C.",
"Jakarta",
"Islamabad",
"Brasília",
"Abuja",
"Dhaka",
"Moscow",
"Mexico City",
"Tokyo",
"Berlin",
"Tehran",
"Ankara",
"Hanoi",
"Manila",
"Addis Ababa",
"Cairo",
"London",
"Paris",
"Rome",
"Cape Town",
"Dodoma",
"Nairobi",
"Kampala",
],
}
df = pd.DataFrame(data)
df = self.add_country_codes(df)
return df
def export_sample_data():
"""匯出範例資料到CSV檔案"""
processor = WorldDataProcessor()
df = processor.create_sample_world_data()
# 儲存為CSV
output_file = "world_data_sample.csv"
df.to_csv(output_file, index=False, encoding="utf-8-sig")
print(f"✅ 範例資料已儲存到: {output_file}")
# 顯示資料驗證報告
report = processor.validate_data(df)
print("\n📊 資料驗證報告:")
print(f"總筆數: {report['total_rows']}")
print("\n缺失值統計:")
for col, info in report["missing_data"].items():
if info["count"] > 0:
print(f" {col}: {info['count']} ({info['percentage']}%)")
return df
if __name__ == "__main__":
# 執行範例
print("🌍 世界資料處理工具")
print("=" * 40)
# 建立並匯出範例資料
df = export_sample_data()
print(f"\n📋 資料預覽:")
print(df.head())
print(f"\n📊 資料摘要:")
print(df.describe())